This is an outdated version of PHPasap. Please visit for the latest version.


What are routes

Routes are a way to tell your application which controller will handle the given url request. So say someone hits /news/all then there should be a routing rule defined in your routes.php (routing file) that tell which Controller method will handle the request for this url depending upon the HTTP verb.

In a very typical MVC framework this routing is handled by framework itself depending upon the requested url. So a request to will be handled by News controller (controller is nothing but a php class) which will have a all method.

However PHPasap doesn't handle the routing implicitly but instead uses user defined routing rules. This gives the developer the ability to handle any request by any controller. So url structure is NOT limited to{controller}/{method} but instead the url can anything and developer will have full control of which Controller will handle the given url request. Read below to learn how to add routing rules.

Adding routing rules

All your routing rules are defined in app/routes.php file.

So a rule for GET request to /news/all can be defined as follows

Route::add("GET", "news/all", "News_Controller@all");

The above line tells the application that whenever a GET request to news/all is made then call the all method of News_Controller class. The News_Controller class will be in app/controller/news_controller.php file. Note how the controller and method name are seperated by @ character in third param of add method

The general format of add method is as below

Route::add($HTTP_verb, $url, $Controller@method);

So to declare a rule for POST request to news/add you would add below line

Route::add("POST", "news/add", "News_Controller@add");

Passing Variable / Capture a url segment

Route::add("GET", "news/view/{id}", "News_Controller@view");

Notice the use of {id} in the second param. If someone hits then {id} will be evaluated to 24 and this will be passed as the first parameter to the view method. Needless to say the view method of News_Controller class must accept atleast one parameter.

public method view($id) {
    echo $id; //for this will be 24

Passing a closure to handle a request

Instead of a controller method you can even pass a closure to handle the request in routes itself.

Route::add("GET", "welcome", function(){ echo "Welcome to my app"; });

While you may feel that it's of no use in a real world app, consider below example

Route::add("GET", "welcome", function(){ echo View::render("welcome"); });

In above example we are using a view file to render the html output. We'll learn more on Views in upcomming sections of documentation.

Similary you may make a database query to in the closure itself

Route::add("GET", "news/view/{id}", function($id){
    $news_item = DB::table("news")->find($id);
    echo View::render("single-news",["news"=>$news_item]);

In last two examples we have handled the request in closure itself avoiding use of a controller. This methodology is useful for static pages.

Controller routing

Sometimes you may want all urls starting with a particular url segments to be handled by a single controller. This is particularly for a CRUD application. Like user/add-profile, user/edit-profile, user/view-profile.. to be handled by User_Controller. Writing a new route url for each of this can be quite tedious. In such case you can use the controller routing

Route::add("CONTROLLER", "user", "User_Controller");

Note that we have

  • Used CONTROLLER as HTTP Verb
  • Instead of Controller@method we have only User_Controller i.e the Controller name

Now any request starting with will be handled by method having name same as first url segment after this user prepended by the HTTP Verb and underscore

  • So GET request user/add-profile will be passed to get_add_profile() method
  • So POST request user/add-profile will be passed to post_add_profile() method
  • So GET request user/delete/{user_id} will be passed to delete($user_id) method

Throwing 404 error

To conditionally show a 404 error you may use the below code.


//OR if you want to show custom 404 page