AzizMVC Documentation
Usage:
- Just like any other MVC application, extract the folder into your webserver and run it. Currently, AzizMVC only supports MySQL data model, so if you are going to use it, make sure you update the /AzizMVC/configs.php file with the correct information. The config file also homes the list of plugins and helpers that you want to be automatically loaded on every controller (for example, template plugin that you will be using for virtually every view).
- A detail description of every directory in the MVC application:
- /index.php
- This is the main file that gets called when the MVC application runs.
- /AzizMVC/*
- This directory contains the main controller, data model controller, the loader (which loads views/models/helpers/plugins) and the configuration files.
- /Controller/*
- This directory will home the controllers for your application. It contains an example, welcome.php to show you how to use it.
- /Helpers/*
- This directory contains the helpers. Helpers are usually files with one function to do a specific task.
- /Model/*
- This directory has all the data model files that you use. It contains and example, welcome.php to show you how to use it.
- /Plugins/*
- This directory contains the plugins. Plugins are similar to helpers, but usually contain more than one function or class/classes.
- /Templates/*
- This directory contains the list of templates that the template plugin uses. Currently the “Default” template is the one set. You can add any template you like to this directory in its own folder.
- /View/*
- This directory contains the views of your website, basically the files with the HTML code. It contains and example, welcome.php to show you how to use it.
Controllers:
- Controllers:
- The first part of the URL query is your controller. So if you call your website using this link:
this will attempt to call the file name:
And will execute the class test within it:
<?php class test extends mainController { public function __construct() { parent::__construct(); } }
Note how all your controller classes must extend the mainController class and must initialize it in the constructor as “parent::__construct();”.
- Each controller must be named as the class name, so the test controller must be in a file called test.php and the class name must be test.
- You need to insure that your controller classes extend the mainController class and that it is initialized in the constructor (see example above).
- Since each controller is unique you can add dynamic info for the page title and similar meta tags, store them in variables for the viewer.
- There must exist a controller for each page, but not a model/viewer. For example a header redirect or download page do not need the view part of it the the former doesn’t need the model either.
- NOTE: controller filenames must be lowercase at all times to insure that it works on all servers, class names on the other hand are case insensitive.
- The first part of the URL query is your controller. So if you call your website using this link:
- Function:
- The controller class constructor are called at all times. If index() functions are there, they are also called upon initialize. To call a function within your class, let’s say sayHi(), you can access the URL like:
- Things your controller have access to:
- The following methods are inherited from the load class:
- The controller class constructor are called at all times. If index() functions are there, they are also called upon initialize. To call a function within your class, let’s say sayHi(), you can access the URL like:
- $this->loadModel(model,handler = null)
- This method will load a model by looking at the model directory. The model must be the file name of the model. If the file is found, it will attempt to look at the class names in the following order: class modelmodel, model_model. If none of the above are found, it will attempt o search the file for the first occurrence of the word class and try to execute the class name after it.
- To access the model functions, you will use $this->model->functionName. If a handler is set, you can do $this->handler->functionName.
- $this->loadView(viewer,variableList, saveOutput)
- This method will load a view from your view directory. It will also pass the variable list to it for usage. If variableList or SaveOutput are true, it will return the view as a variable. Variable list must be an array.
- $this->loadHelper(helperName)
- This method will load a helper for you. Helper file names can be either helper.php or helper_helper.php
- $this->loadPlugin(pluginName)
- This method will load a plugin for you. Plugin file names can be either plugin.php or plugin_plugin.php
- The following variables are inherited from the mainController class:
- $this->model
- This variable contains the data model handler if a custom specific handler was set at model load.
- $this->baseBath
- This variable will contain the absolute path to the MVC application. Alternatively, you can use the constant BASEBATH instead.
- $this->site_url
- This variable will contain the website URL including the index.php/.
- $this->base_url
- This variable will contain the website URL to the MVC application (without the index.php/).
- The following Methods are inherited from the MainController Class:
- $this->segments(index,doClean = true)
- This method will return the list of URL query string separated by the backslash starting from the index.php being at 0. If the doClean is true (by default it is) it will run a mysql_real_escape_string style escape along with turning the equal sign into its urlencode counterpart (this helpers with SQLi).
- $this->getPost(name,doClean = true)
- This method will return the name index of $_POST variable and run a clean (same as with this->segments).
- $this->getGet(name,doClean = true)
- This method will return the name index of $_GET variable and run a clean (same as with this->segments).
- $this->escape(input)
- This is the actual method that gets called for the cleaning process (in the previous three methods).
- This might look hard at first glance, but take a look at the video tutorial to see how easy it is to implement.
Models:
- The data model side is pretty much simple. To create a model, let’s say called mymodel, create a file called mymodel.php in the model folder and declare it’s class like so:
<?php class mymodel extends model { public function __construct() { parent::__construct(); } public function printUsers() { return mysql_fetch_assoc(mysql_query("SELECT * FROM USERS WHERE userID='1'",$this->getLink())); } }
Note how all your model classes must extend the model class and must initialize it in the constructor as “parent::__construct();”. The class name can also be in this form: mymodelMode, mymodel_model. If the class name doesn’t match any of those it will open and search the file and attempt to read the first class name and use it.
- To actually call your model from your controller, you will do this in your controller file:
<?php class test extends mainController { public function __construct() { parent::__construct(); // Load the model from above $this->loadModel('mymodel'); print_r($this->model->printUsers()); // Another way to do the same thing $this->loadModel('mymodel','me'); print_r($this->me->printUsers()); } }
The above controller will connect to the model and print the user lists.
- Your model always has access to the following methods, inherited from the model class:
- $this->getLink()
- This is the connection link to the database. You can use as many connections as you need from your controller. Each one has their own connection link.
- $this->closeLink()
- This closes the connection link. It is a good coding practice to close your connections after you are done using them.
- Take a look at the tutorials (or the extension section) to see the database class I have created that will make your coding much easier.
Viewers:
- The viewer files are much more like what you encounter using your PHP programming without and MVC. It is a good coding practice to not do PHP programming other than if statement, loops, and simple arithmetic on the view section. Most (if not all) of your PHP code should be done in the controller.
- Passing variables from your controller to your viewer is easy. Let’s say we have this controller:
<?php class test extends mainController { public function __construct() { parent::__construct(); $data['testVar'] = 'TestMe'; $data['testArray'] = array('I','am','me'); // Load the Viewer and pass it the variables $this->loadViewer('mypage',$data); // If you want the viewer to be stored in a variable rather than just printed, you do: $pageContent = $this->loadViewer('mypage',$data); echo $pageContent; } }
Now in your viewer file, you can get the variables as such:
This is static HTML text. <?php // This will echo "TestMe" echo $testVar; // This will print "I am me " foreach($testArray as $item){ echo $item." ";}
- You can also treat dynamic css/javascript files as viewers where you load them, send them variables and store them in variables to output them in your header.
Extensions:
- This MVC application can be extended easily by adding plugins or helpers. Both as the same, but for organizational purposes, helpers are those extensions with one function, while a plugin can have more than 1 function or class.
- You can call your helpers anywhere in the application, but preferred to be in your controller. To call a helper in your application you do:
<?php class test extends mainController { public function __construct() { parent::__construct(); // Load BBCode Helper $this->loadHelper("bbcode"); echo makeBBCode("[b]Test[/b] [php] PHP Code [/php]"); } }
- It is recommended that in all your extensions you check if the class/function exist before to avoid common multi include problems.
- If you see that some plugins and/or helpers might be used most of the times, it might be better to have the MVC automatically load them at run time for you. To do this, update the configs.php file:
<?php // Database information . . . /* Plugins/Helpers Auto Loads */ /* Plugins */ $MVC_Configs['pluginAuto'] = array('template','database'); /* Helpers */ $MVC_Configs['helperAuto'] = array(); // End of file /AzizMVC/configs.php<br />
As you see from the above example, currently the template and database plugins are loaded at all times. So if I wanted to use them I would just call the function/class name without loading them manually.
- Visit the download section / Extensions section for a list of all AzizMVC extensions.