The routes.php
file contains the map of all controllers of your web application.
It is where you indicate how the user will interact with your controllers.
The term routing refers to how the web application will select (or map) a certain URL based on its value (or path), and then execute its mapped controller
for that URL. For example, you need to define which controller to be used as landing page (or homepage), or which controller will be used for /blog
path.
In FrostMVC, routing works by registering your controller file to a function in routes.php
file. The function name will serve as the path in the URL.
Basically, FrostMVC uses the following URL format:
https://<domain.tld>/[routeFunc]/[controllerFunc]/[param-1]/[param-2]/.../[param-N]
Take note that in FrostMVC URL format:
routes.php
file.To learn more about how the URL paths will be routed to the controller, take a look at the next topics below.
Routing your controller is as simple as creating a function in app/routes.php
file, and loading your controller using View::route()
function.
Take a look at the following example to see how FrostMVC process the URL to call a certain controller using a route function.
https://www.mysite.com/blog
routes.php
file with the same name as the path from the URL.
View::route()
method.
index()
function of the said controller.
index()
function is a special function that will be executed by the controller if the URL only contains the route function name.
The second path from the URL determines what function in your controller will be executed. Take a look at the following example to see how the framework pick the controller's function based on the URL's path.
https://www.mysite.com/blog/latest
routes.php
file with the same name as the first path from the URL.
View::route()
method.
$pages
parameter).
blog
) and second path (controller function latest
), the framework will look for the function
latest
that has no parameter.
Invoking a function with parameter is not much different than invoking a function without parameter.
The only difference is that the values of the parameters will be acquired respective with the URL's third path onwards.
Take a look at the following example:
https://www.mysite.com/blog/view/1145/frostmvc-routing-tutorial
routes.php
file with the same name as the first path from the URL.
View::route()
method.
$pages
parameter).
$articleid
(which is now holding the string value '1145'
and the fourth path will become the value of the parameter $slug
(which is now holding 'frostmvc-routing-tutorial'
string value.
Generally in PHP, you can declare a default value for your parameter if you want it to be optional.
class BlogController extends Controller{
public function index(){
print '<h1>Hello World!</h1>';
}
public function latest(){
print '<h1>Welcome to Latest Blogs!</h1>';
}
public function view($articleid, $slug = false){
vardump([$articleid, $slug]);
}
}
In the code above, the parameter $slug
is optional. This will make the following URL valid for the function.
https://www.mysite.com/blog/view/1145/frostmvc-routing-tutorial
https://www.mysite.com/blog/view/1145
https://www.mysite.com/blog/view
$articleid
is not an optional parameter.
In order to specify which controller is the default controller for landing page (or homepage), there is a special function in routes.php
file that can be used for it.
The index()
function in routes.php
file can be used to specify the controller for your homepage.
function index(){
View::route('HomepageController');
}
$pages
is no longer required in index()
function.HomepageController
, you can only specify index()
function to get the which view file will be loaded as homepage.class HomepageController extends Controller{
public function index(){
View::get('home');
}
}