Skip to content

Routing

Alvin Lal edited this page Mar 14, 2021 · 4 revisions

Routing

Routing refers to how an application’s endpoints (URIs) respond to client requests. You define routing using methods of the alvin\phpmvc\Application object that correspond to HTTP methods.

The following code is an example of a very basic route.

//index.php

<?php

include_once './vendor/autoload.php';

use alvin\phpmvc\Application;
use alvin\phpmvc\Request;
use alvin\phpmvc\Response;

$app = new Application(__DIR__); // root directoy path must be provided

// respond with "hello world" when a GET request is made to the homepage
$app->get('/',function (Request $request, Response $response){
    return "hello world";
});

$app->post('/',function (Request $request, Response $response){
    return "handling post request";
});

$app->run();

alvin\phpmvc\Request and alvin\phpmvc\Response containing usefull methods will be provided to the controllers

👉 Only GET and POST methods is supported now, other methods like UPDATE, DELETE,PUT will be included in future update.

Route params

Route params can be defined by enclosing the parameters inside {}

//index.php

<?php

include_once './vendor/autoload.php';

use alvin\phpmvc\Application;
use alvin\phpmvc\Request;
use alvin\phpmvc\Response;

$app = new Application(__DIR__); 


$app->get('/{id}',function (Request $request, Response $response, $id){
    return "passed in parameter is $id";
});

$app->run();

The passed in parameter will be made available on the controller as the last arguments.

Any number of arguments can be passed

$app->get('/{username}/posts/{id}',function (Request $request, Response $response, $username, $id){
    return "showing post with id $id of user $username";
});

👉 parameters will be passed to the controller in the order they are declared in the route.

Route handlers

A anonymous function, name of a view, or a array containing name of Controller class and a method inside it can be set as a route handler.

Anonymous function

We have been using anonymous functions as route handlers in all the above example

$app->get('/',function (){
    return "hello world";
});

Name of a view

Name of a view that is located inside the views folder can be passed, that view will be rendered and returned when route is requested by the client.

👉 The view must be located inside view folder inside the root directory of the application.

$app->get('/','index'); // Renders index.php inside views folder and returns html to the client

Controller class and method

An array containing name of the controller class as the first element and method name inside the controller class as the second element can be passed. The specified method will be executed when corresponding route is requested by the client.

//index.php

<?php


include_once './vendor/autoload.php';

use alvin\phpmvc\Application;
use app\controllers\SiteController;

$app = new Application(__DIR__);

$app->get('/',[SiteController::class,'index']); // will run SiteController::index when this route is invoked

$app->run();

See Controllers for more info.

// controllers/SiteController.php

<?php

namespace app\controllers;

class SiteController {
	public function index() {
		return "hello world";
	}
}

Clone this wiki locally