MVC, HMVC, and how it works on FuelPHP
We will now look into one major aspect of the FuelPHP framework – the MVC and HMVC software architecture patterns.
Model-view-controller (MVC) is a software architecture pattern that states that the code should be separated in three categories: models, views, and controllers.
For those who are not familiar with it, let's illustrate this through an example:
Suppose a user tries to access your website. The following are some URLs he/she might request:
http://my.app/
http://my.app/welcome/
http://my.app/welcome/hello
Depending on the requested URL, your website is generally expected to return some HTML code and it also sometimes needs to update the database, for instance when you want to save the users' comments.
The returned HTML code is generated by the views, because this is what is received by the browser and indirectly seen by the user.
The database is generally updated through models. In concrete terms, instead of executing raw SQL code to access and update the database, the best practice is to use classes and instances to do so. Each class represents a model that is related to a specific table: for example, the car model would access the cars table. Each class' instance is a model instance linked to a specific row in a table: for example, your car's information can be saved as a car instance that will be linked to a specific row in the cars table. As we use classes instead of raw SQL code, the framework has already implemented frequently needed features such as reading, creating, saving, or deleting model's instances. A further advantage is that, as we used packaged and well-implemented methods to access our database, it can prevent most unintended security breaches that we can create when requesting the database using raw SQL.
The controllers allow the website to handle the user's request by selecting the correct view to send back (the response) and updating the database (through models) if necessary. Controllers handle a specific section of the website: for instance, the car controller will handle everything that is related to cars. Controllers are subdivided by actions that will handle specific features: for instance, the list action of the car controller will return a list of cars in HTML code. In practice, controllers are classes and actions are methods.
When the user requests a URL, the framework will select an action inside a controller to handle it. Those are generally chosen by convention; for instance, when requesting http://my.app/welcome/hello, the framework will choose the hello action inside the welcome controller. Sometimes, they can also be chosen using a routes configuration file that matches URLs to actions and controllers.
The views sometimes need to access models; for example, we need to access the car model's instances when we want to display a list of cars. However, views should never update models or the database; only the controllers and preferably models should do that.
Please note that additional code components as helpers or presenters can be added to ease the development process, but if you understood this section, you got the most important points.
Let's illustrate how it works by testing our newly created website. We suppose that your application is available at the following URL:
http://my.app/
If you request a random URL, you will probably get a 404 exception. For instance:
http://my.app/should_display_404
But, if you request the following URL, you will display the same page as the home page:
http://my.app/welcome/index
If you request the following URL, you will display a different page:
http://my.app/welcome/hello
Let's first explain how the last two requests worked. You can notice that both URLs contain the welcome word just after the base URL. You can also find this word in the file name fuel/app/classes/controller/welcome.php; it turns out that welcome is a controller. Now, open this file using your preferred text editor. You will then read the following:
You can notice the action_index and action_hello methods. These functions are called actions. Now, as you have probably guessed, when you request http://my.app/welcome/index, the action_index method will be called. In a more general manner, if you request http://my.app/CONTROLLER/ACTION, the action_ACTION method of the CONTROLLER controller will be called. Let's test that. Edit the action_index function to add a simple echo at the beginning:
Now, if you request http://my.app/welcome/index, you will read the printed content at the beginning of the web page. Though this is an easy way to test how things work, never print anything in your action or controller. When you print a message, you are already implementing the view entity; thus, printing something in the controller breaks the MVC pattern.
But then how are the pages rendered? Let's analyze the only line of code in our index action:
View::forge('welcome/index') returns a View object generated from the fuel/app/views/welcome/index.php view file. We will use this function a lot in this chapter and this book, and will cover all its parameters, but you can read its official documentation in the FuelPHP website:
http://fuelphp.com/docs/classes/view.html#/method_forge. (It can be accessed through the FuelPHP website by navigating to DOCS | TABLE OF CONTENTS | Core | View)
Response::forge(View::forge('welcome/index')); returns a response object created from the View object. Additional parameters allow us to change headers or the page status. A response object contains all the necessary information that will be sent to the browser: the headers and the body (generally the HTML code). You are recommended to read the official documentation on the FuelPHP website at http://fuelphp.com/docs/classes/response.html#method_forge (It can be accessed through the FuelPHP website navigating to DOCS | TABLE OF CONTENTS | Core | Response)
Since the view is generated from the fuel/app/views/welcome/index.php file, open it to discover its content. You can notice that this is the same HTML code as the one displayed when requesting the URL. Just after <h1>Welcome!</h1>, add <p>This is my first view change.</p>. Now, if you refresh your browser, you will see this message appear under the Welcome! title.
It is possible to indicate parameters, both to the actions and to the views. For instance, replace your index action by the following code:
And in the fuel/app/views/welcome/index.php view file, replace
by
Now, if you request the following URL:
http://my.app/welcome/index
the title will display Welcome user (id: 0)!
If you request the following URL:
http://my.app/welcome/index/Jane
the title will display Welcome Jane (id: 0)!
And if you request the following URL:
http://my.app/welcome/index/Jane/34
the title will display Welcome Jane (id: 34)!
You might have understood that if you request the following URL:
http://my.app/CONTROLLER/ACTION/PARAM_1/PARAM_2/PARAM3
The action_ACTION method of CONTROLLER will be called with the PARAM_1, PARAM_2, and PARAM_3 parameters. If there are less parameters defined in the URL than required in the method, either, if defined, the parameters take their default values (as illustrated previously), or, if no default value is defined, it will trigger a 404 error.
You can notice that we replaced
By
View parameters are sent by the second parameter of \View::forge in an associative array. Here, the associative array has two keys, name and id, and their values are available inside the view file through the $name and $id variables.
In a more general manner, if you call the following:
When the view file will be executed, parameters will be available through the $param_1 and $param_2 variables.
Though what we previously observed explains how the standard cases operate
http://my.app/CONTROLLER/ACTION
we haven't explained why the two following URLs return content though no associated controller and action can be found:
http://my.app/
http://my.app/should_display_404
For understanding why we have to open the fuel/app/config/routes.php configuration file:
You can first notice the following two special keys:
_root_: This defines which controller and action should be called when requesting the website root URL. Note that the value is welcome/index, you can now understand why http://my.app and http://my.app/welcome/index are returning the same content._404_: This defines which controller and action should be called when throwing a 404 error.
Beside specials keys, you can define the custom URLs you want to handle. Let's add a simple example at the end of the array:
Now, if you request the following URL:
http://my.app/my/welcome/page
it will display the same content as in the following URL:
http://my.app/welcome/index
You have probably noticed that there is also another key already defined: hello(/:name)?. The routing system is quite advanced, and to fully understand it you are recommended to take a look at the official documentation:
http://fuelphp.com/docs/general/routing.html (It can be accessed through the FuelPHP website by navigating to DOCS | TABLE OF CONTENTS | FuelPHP | Routing)
You might have seen that the hello action doesn't use the View class to display its content, but instead it uses the Presenter class:
Let's analyze what is happening in this case. First, you can notice that, as for the views, a view file exists at the following path: fuel/app/views/welcome/hello.php. If you open this file, you will see that the code is the same as the one displayed when requesting the URL http://my.app/welcome/hello, except for one tiny difference. You can find the following code:
In a normal view, we would have to define the name parameter, except here we didn't. Though, when displaying the web page, this parameter seems to have a defined value (it displays Hello, World!). Where could it be defined then?
Probing a little further, you can find another file located at fuel/app/classes/presenter/welcome/hello.php. It contains the following:
This file contains a Presenter class. The view function is called before rendering the view and it is here that the name parameter is set. It tries to get the name from the request parameter, name, but if it is not defined, the default value is World.
If you wonder how to change this parameter, refer to the routes. For instance, request the URL http://my.app/hello/Jane.
One could then wonder the use of Presenter classes, since we could change the previous code into a more classic view and controller approach.
Let's show its usefulness by an illustration. Suppose you have created an internal website managing the clients of your corporation. Each client is associated to a client category. In your creation, edition, and other forms, you thus display a selectable list of client categories. Each time you display the exact same selectable list, though you access it by using different controllers and actions. You can come up with three solutions:
- You can create a classic view for your selectable list, load the list of client categories inside each of your actions, and pass this list to each view until you reach the location where you want to display your list. The problem is that it would induce a lot of code repetition.
- You can create a classic view and load the list of clients inside this view. This way, you wouldn't have to pass along the necessary parameter. The problem is that you would break the MVC pattern by mixing models and views.
- You can create a
Presenter class, load the list inside the Presenter class, use it inside the view file, and display the view file using Presenter::forge. This solution is the best because it doesn't mix views and models but still limits the code duplication.
The oil utility and the oil console
The oil utility is a very handy command-line tool. As the rails utility of Ruby on Rails, oil allows you to do the following:
- Easily generate code files: models, controllers, migrations, and entire scaffoldings
- Run tasks and migrations
- Easily install, update, or remove packages
- Test your code using PHPUnit test or a real-time console
- Even run a PHP-built-in web server hosting your FuelPHP application (for PHP >= 5.4)
Though we will use all these features, except the last one in this book, we recommend that you take a look at the official documentation at:
http://fuelphp.com/docs/packages/oil/intro.html (It can be accessed through the FuelPHP website by navigating to DOCS | TABLE OF CONTENTS | Oil | Introduction)
In this section, we are going to use the oil console, which is an important tool if you want to test your website, or, as in this case, a FuelPHP feature.
First, open your command-line utility and go to the root of your website directory. Then, enter the following line:
Tip
If you use a web development platform such as WAMP or MAMP, you are recommended to use the PHP executable inside the platform directory for launching the oil utility (it might not work otherwise). As I wrote this book, this executable is located at WAMP_DIRECTORY\bin\php\phpVERSION\php.exe for WAMP, and at MAMP_DIRECTORY/bin/php/phpVERSION/bin/php for MAMP (VERSION depends on the version of PHP you installed, the best is to check this directory by yourself using a file explorer).
This will open the command-line interface oil provides. When you press Enter, something similar to the following should appear:
You can now type any PHP code and it will be executed. Let's start with something simple:
If you press Enter, nothing will be printed, but the $a variable will be set to 2. Now, if you want to check a variable value, simply enter its name and then press Enter:
It also works for more complex variables:
But be aware, that you might have trouble displaying complex objects.
Let's now test a FuelPHP feature. Earlier, when discussing the app directory structure, we explained that the configuration files in the fuel/app/config directory were merged with the ones with the same filenames in the fuel/app/config/ENV directory, ENV being FuelPHP's current environment. We will now test this behavior.
First, let's check FuelPHP's current environment:
The environment should be set to development.
Now, create a PHP file located at fuel/app/config/test.php where you will write:
Then create another PHP file located at fuel/app/config/development/test.php and write the following:
and an additional one located at fuel/app/config/production/test.php, where you will write the following:
Now, if you return to the command-line interface, you can load the test configuration file by writing the following:
You are recommended to read the Config::load official documentation for more information at:
http://fuelphp.com/docs/classes/config.html#/method_load. (It can be accessed through the FuelPHP website by navigating to DOCS | TABLE OF CONTENTS | Core | Config)
As explained before, the value returned will be a mix of the fuel/app/config/test.php and the fuel/app/config/development/test.php configuration files:
If we change the FuelPHP environment to production:
And load again the test configuration file:
The merging will be done with the configuration file in the production folder.
Note
You have probably noticed that we added a third parameter for Config::load. This parameter allows you to clear the configuration cache. If we didn't set it to true, the method would have returned the old configuration we loaded when we were in the development environment.
But what happens when the fuel/app/config/production/test.php and fuel/app/config/test.php configuration files contain the same key? The console can find the answer for us.
Change the content of the fuel/app/config/test.php configuration file to the following:
and change the content of the fuel/app/config/production/test.php configuration file to the following:
Let's now reload the test configuration files as follows:
It is interesting to analyze how the preceding two configuration files have been merged:
- The
this_is_the_root_config_file key shared by the two configuration files is associated in both cases to a simple value. In the resulting configuration, it is the value from the production file that prevails. - The
complex_value key is associated in both cases to an array. The two arrays seem to have been merged in the resulting configuration.
This is because the configuration files are not merged by the array_merge native PHP function, but instead by the Arr::merge FuelPHP function, which merges arrays recursively. You are recommended to take a look at its official documentation at http://fuelphp.com/docs/classes/arr.html#/method_merge (It can be accessed through the FuelPHP website by navigating to DOCS | TABLE OF CONTENTS | Core | Arr)
It should be clear now that the console is a great tool that allows you to test your application. It can also be used as a great complement to the documentation, as you can try FuelPHP methods and their parameters without changing any files in your application.