PHP Controllers

Controllers are the first point in your application that is called during an incoming request.

Types of Controllers


MogulMVC has 3 different types of controllers. Action, API, and default.

Normal Controllers

Lets start with the normal controllers. They are files located in the backend controller directory which are named after and contain a class matching the first segment of a URL being requested. For example, if site.com/users/profile was being requested a controller called Users.php would be invoked. This is untrue of Action and API controllers.

Actions and APIs

Action controllers work differently than normal controllers. Inside the config/config.php file there is a setting called APPLICATION_ACTION_URL. If the first segment of the URL is this string an Action controller will be served. Action controllers aren't classes. They are script which do one action and never serve up an HTML page. They were designed to be where AJAX and form actions are sent. Actions should always accept their variables as $_POST variables.

API controllers work similarly to Action controllers. Their routing is controlled by the APPLICATION_API_URL constant defined in config/config.php. APIs are also not classes and generally will output XML or JSON data. They are for allowing AJAX calls to get new data from a databased. APIs should always accept their variables as $_GET variables.

Create


Controllers are classes named the same as the first segment of the URL being served. The second segment of the request is served by a function within the class. If there is no second segment the request is served by a function called index within the controller class.

So assume we want to make a controller to serve a request for a page at site.com/blog/top50. We would first make a controller called Blog.php containing a class of Blog.

Class Blog{
				
}

It's always a good idea to create a index function in case the second segment is not filled.

Class Blog{
				
	function index(){

	}

}

Next we create a function called top50.

Class Blog{
				
	function index(){

	}

	function top50(){

	}

}

Any code inside the top50 function will be run when site.com/blog/top50 is requested.

How Controllers are Invoked


Because MogulMVC has 3 different types of controllers their are 3 different ways controllers are invoked. We will first focus on normal controllers, then discuss actions and apis together as they are invoked very similarly.

Normal Controllers

Assume you are requesting a page at site.com/user/profile. This page would be served by the User.php controller. Notice that the first u in Users.php is capitalized. This is required because this file contains a class called Users and a class should always have a capital first letter. The second segment of the URL would be serviced by adding a function inside the class called profile. Notice that this function is not capitalized.

If the first segment of the URL is empty the APPLICATION_DEFAULT_CONTROLLER will be served. This file is configured in config/condig.php.

If the second segment of the URL is empty the default function will be index.

If the controller, function, or route to fulfill the requested URL exist a 404 page will be served.

Actions and APIs

Actions and API controllers are served differently as discussed above.

Actions are served based on the APPLICATION_ACTION_URL constant declared in the config/config.php file and APIs are served based on the APPLICATION_API_URL.

Because Action and API controllers are served in a very similar manner we will be discussing them together.

Assume the URL site.com/action/post_update were being requested. You might think this would be served by a controller called Action.php, but it wouldn't be. By default the APPLICATION_ACTION_URL is “action” and so this request would be a request for an action controller called post_update.php.

This is similar if you are requesting site.com/api/get_posts. Because APPLICATION_API_URL is “api” by default this would serve an API controller called get_posts.php.

If you were to change the APPLICATION_ACTION_URL or APPLICATION_API_URL to a different string then site.com/action/post_update and site.com/api/get_posts would be served by controllers called Action and Api respecivly.