avatar

Michaël Duerinckx

 

Posts Tagged ‘PHP’

MVC – FuelPHP

Since I started working at Carswell Gould, I’ve learnt a lot more about web development—It’s amazing how much you learn once you start collaborating with other people—and among those things I learned is how to work with the MVC (Model-View-Controller) design pattern. The way I used to work was just by having the database operations inside classes as much as possible, and use those classes and their methods in the files for the actual pages. For example, A forum thread page (view.php or similar) would have all the code to load the posts and relevant info above the HTML part of the file, using classes like ForumThread and the like.

The MVC way to go for this would be having a controller that gets called when the URL for viewing a thread is requested. The controller would get an instance of a ForumThread Model. The Model describes the properties of a ForumThread and deals with related data (Forum posts, which would be another Model), and database operations, plus any additional behaviours. The data retrieved from the Model instance then gets passed on to a View, which takes care of sticking all the data in HTML. This way interpreting the request, dealing with data and displaying data is nicely separated.

Frameworks (FuelPHP)

This whole design pattern is all a very beautiful way to structure your web applications, however, if you have to set that all up from scratch, it would get very tiresome to get right. That’s where frameworks come in. The first PHP framework I got in touch with is FuelPHP, a quite young framework running on PHP 5.3+. According to their front page, it is based on the best ideas of other frameworks, with a fresh start. As this is the first framework I’ve used, I can’t really judge that former part, but by googling certain concepts used, I’ve found out that many of them are quite commonly accepted ones.

In my experience, it’s very simple to set up some new models and relate them, to quickly get going without all the design hassle you’d normally have. For the most part, you don’t get to write the database queries yourself, which is a good thing.

Validation

An unrelated part of FuelPHP (as in, it doesn’t really have anything to do with MVC) is its validation functionality. FuelPHP makes it really smooth to validate input. There are multiple ways to go for it, but my preferred way is to actually specify the validation rules in a Model’s properties array, then setting up a Validation Observer before save.

If that sounded confusing: An observer is a class that has a number of event-named methods. In a Model you can refer to an observer class and tell it which events to observe. FuelPHP takes care of the rest: When you let your model save its data to the database, any observers with ‘before_save’ enabled will be executed first. In the case of a Validation observer this will first run all the validation rules specified in the properties set-up, and throw an error (preventing the actual database save) in the case that some data does not validate.

Having gone through setting up a whole bunch of nested ifs for data validation in the past (Believe me, it’s tiresome and not elegant at all), I find this way of setting up validation simply beautiful. It’s all down to adding a few strings to an array, and it’ll validate. The way the framework is built allows for easy adding of your own validation functions too, so other than elegant, it’s very flexible.

Adopt FuelPHP!

Currently, the community surrounding FuelPHP is still fairly small, which means it’s often quite hard to google FuelPHP-related things. I would very much like the community for this framework to grow, as I feel it has a lot of potential. Their website (mostly docs section) is still quite incomplete in places, but that should improve as the community grows.

More information

I’ve only briefly touched upon many subjects in this post, so if I sparked your interest, these couple of links should get your started.

Obviously, if you are really interested in trying it out, I suggest downloading it and playing with it! You can get support on their forum and in their IRC channel.

A collection of web development links

Over the time I got into web development, I’ve headed to google a lot. This has resulted in running in to countless handy resources I didn’t know of before.

In this post I’ll try to make a summary of a good set of links regarding webdev. If it’s not obvious, the paragraph headers link to the pages I’m talking about.

1. HTML Dog

Note: my first one used to be W3Schools, but I found out that W3Schools has multiple cases of misinformation in their tutorials, as is more thoroughly explained here.

On Html Dog, you can learn the basics of HTML and CSS, and go from there. This is a lesser known tutorial site, but is very clear in it’s explanations, and more technically accurate.

Whenever I run in to a good site that gives an introduction to PHP or JavaScript, I’ll add it on here as well, since the removal of W3Schools takes those away.

2. PHP.net

If you’re developing pages in PHP, and need to know what the order of the parameters of the explode function was, you can find it here. PHP.net has a really nice manual for every function that’s built in the language, and trust me, that’s a lot. The manual goes in detail about what the functions do, with added examples to help clarify. What helps is that when you just type ‘php’ [function name] in the url bar and hit enter, it takes you right to the function page 95% of the time. That is, if you’re using a decent web browser.

3. jQuery

jQuery is with good reason the most popular JavaScript library. It does exactly as it promises: write less, do more. Using jQuery makes JavaScript fun again. jQuery is not only easy to learn, it also has a whole lot of support. At api.jquery.com, you have the whole manual at your fingertips. I can comment the same thing on the jquery manual as I can on PHP’s. The same thing goes here as well, type ‘jquery’ [function name] in the url bar, and you’re there in seconds.

4. AjaxLoad

Once you’re starting to have a decent grasp of your (x)HTML and JavaScript (and perhaps jQuery), you will probably get into ajax.  Ajax basically is a technique that allows you to send/receive date to/from the server, without having to load a whole page. Since using this technique will often cause the user to have to wait for a little while, it’s always handy to show a little spinning animation, so the user doesn’t get the idea it’s not responding. You’ve probably seen them all before, but where do they come from? On this website, you can easily generate one of these nice gifs, in custom colours. It’s straight, to the point, and very handy.

5. Favicon.cc

Every decent website has its own custom icon that show up in the url bar and in the tab its loaded into. Such an icon is called a ‘favicon’. On this site you can import an image to convert it to an icon, and you can alter it after that. That or you can just start from scratch and start clicking pixels on the neat JavasScript-driven web app.

6. JsFiddle

I found out about this little gem not so long ago, and I’ve fallen in love with it. JsFiddle allows you to- you guessed it- fiddle with JavaScript. The interface is as simple as it can get, yet it must have quite a complex background engine. You get a HTML editing area, a CSS area, and a javascript area. The fourth area is the resulting webpage. This website saves you the hassle from saving/refreshing the whole time with a text editor/browser combo. Just a click on the “run” button suffices here. The app also has a really nice tidy up function, which fixes all your indentation failures. On top of that, you can choose to use one of the javascript libraries that are available, which makes jQuery testing handy. And then you can save your fiddles as well, so you can share it with others. The update button makes sure you don’t mess up what you made completely, as it saves it as a new version of the fiddle automatically. I love this site.

7. Pastebin

Pastebin is a handy tool for collaboration. Basically, you can paste your code snippet, and select which language it’s in. It’ll automatically highlight the syntax accordingly. You can select how long you want to have the entry up on the site, and then you can share it. It has some handy embed and download functions as well. I think this is mainly handy for sharing code, showing examples, and letting other people check your code.

8. Color scheme designer

This beautiful website helps you out a lot when you’re failing at selecting good colours for your design. It has a variety of options to build fresh looking colour schemes. It has many features for things you wouldn’t even think about.

That about wraps it up for this post, I think. If I’ve missed any significant web apps/resources, please let me know. I’ve only included those I personally have experience with, so it’d be great if you could introduce me to some others as well.

PHP class: Page control

Time for my first coding-related post!

Whenever I was coding something in php, and I had to make something with a page control, I started procrastinating, because I was always looking up against figuring that stuff out again.

I decided to get that over with for one and all, and wrote this page control class that allows me to create a new, fairly customizable page control, which can even be used to interact with the code that loads whatever needs to be displayed on the page.

It is only the first version, which only allows for one type of page control (the typical first, previous, next, last controls), but I’m planning to make it do different types in next versions. Anyway, that’s enough rambling, it’s time for some code. (more…)