Routes

The routes can be managed in a traditional way where the last part of the route can be the controller or a function of the controller.

The following route:

blog/post/

Would load the post.php file inside the blog folder in the controller path.

So, the following route:

blog/post/sayHi

Will call the sayHi function in the post controller class if it exists.

# Add

Routes can also be managed in a cleaner and modern way. In the system/Definitions/Routes.php file you can define routes, its parameters and what to do.

The first parameter is the desired route, the second is the function that will be executed when accessing to the route, the third and optional parameter is the HTTP return code.

The following block of code can be added to Routes.php:

Route::add('main_page', function() {
	$this->load->controller('home');
});

The same route but with a HTTP 301 response code:

Route::add('main_page', function() {
	$this->load->controller('home');
}, 301);

It will load the home controller when accessing to example.com/main_page, obviously you can add more stuff to the function.

The following block of code:

Route::add('main_page/hi', function() {
	$this->load->controller('home/sayHi');
});

Will load the sayHi function in the home controller when accessing to example.com/main_page/hi

Load a controller directly

You can load a controller in a easier way if you pass a string as the second parameter. It'll be taken as the name of the controller which will be loaded.

Route::add('main_page', 'home');

It will load the home controller when accessing to example.com/main_page.

# Add with parameters

You can use get parameters in the URL

The following block of code

Route::add('main_page/{name}', function() {
	echo $_GET['name'];
});

Will take the second part of the route as a get variable which you can get from the common $_GET array using its name as the key, variables should be put between brackets and only be alphanumeric characters.

# API

You can define API routes with the api method.

It is the same than the add function with the difference that this will set the header Content-Type to application/json.

The first parameter is the desired route, the second is the function that will be executed when accessing to the route, the third is the HTTP response code.

Making a simple API GET route that returns a user information based on its id:

Route::api('user/{id}', function () {
    echo DB::run("SELECT * FROM users WHERE user_id = ?", $_GET['id'])->toJson();
});

# Block

Routes can also be blocked using the block function. When blocking a page if anyone tries to gain access to it, they will be redirected to the 404 page.

The following block of code

Route::block('main_page');

Will block the access to example.com/main_page only

# Block recursively

You can block any sub route using the * symbol

Route::block('main_page/contact/*');

This will block any route that has access to any main_page/contact subdirectory, keep in mind that main_page/contact itself will still be accessible

# Redirect

You can redirect one route to another. When doing it, a 301 HTTP response code will be returned.

Route::redirect('page1', 'page2');

With this the route of page1 will redirect to page2, you can specify an HTTP response code if you want.

Route::redirect('page1', 'page2', 200);

Will do the same but returning a 200 HTTP response code.

# Methods

The Route class has some useful methods that you can use.

exists

Returns true if a view exists, false otherwise.

Route::exists('home');
//Will return true if the home route exists

getRoutes

Returns all the available routes.

Route::getRoutes();

getRedirects

Returns all the available redirections.

Route::getRedirects();

getBlocked

Returns all the blocked routes.

Route::getBlocked();

Keep in mind that all these get methods return an associative array with the results.