I'm starting to study this architecture and came across this term: routing
.
edit
Do I need some framework
to build on this architecture?
I'm starting to study this architecture and came across this term: routing
.
edit
Do I need some framework
to build on this architecture?
"Routing"
translating is "Roteamento"
.
By what I understand from MVC, the term Routing is related to the control / definition of routes of an application.
These routes are usually used to define an action that will be performed when a particular url is accessed.
Example: redirects, response in json
or html
.
It is very common to use routes in PHP frameworks that use the MVC standard.
I will give an example of route definition in the framework called Laravel.
Example:
Route::get('/', 'HomeController@getIndex');
Route::get('/about', 'HomeController@getAbout');
Basically, in the above statement, you are defining two routes. Each one performs a different action, for each path
different ( path
is the part of the url that stays after /
).
That is, when someone accesses url meusite.com.br
the method getIndex
of HomeController
will be invoked. However, if you access meusite.com.br/about
, getAbout
will be invoked.
In addition to the term Routing , I could not fail to mention that the Symfony and Laravel frameworks use this term Routing
internally in their namespaces
.
These frameworks are separated by several libraries. And in one of these, there is a Routing call. This is because the developers of these two frameworks had the idea of creating the entire separate routing structure in a specific library.
It is important to mention that the functions assigned to the routing of the libraries mentioned above go beyond a definition of an action for each url. In the routing systems of these frameworks, other responsibilities are assigned to that library. These are:
Defining filters. A route can only be accessed according to a condition. For example, an authenticated user can access a a
route, but an unauthenticated user can not.
Collection of routes. A class responsible for logging all route information.
Error handling. If the url accessed represents an undefined route, a default action should be called.
Verification of the request verb. In this case, the question is whether the route accepts POST, GET, PUT, PATCH, DELETE, etc. requests. It is useful to do this check for Restful applications.
Symfony and Laravel% s library
The libraries listed above are currently in Github. If you want to take a look at how the organization was organized, take a look at them:
PHPLegends% Library
The Routing
library has a very simple way of creating a routing system, without dependency on a framework.
Example (similar to previous example):
include __DIR__ . '/vendor/autoload.php';
$router = new \PHPLegends\Routes\Router;
$router->get('/', function () {
return 'Hello World';
});
$router->get('/about', function () {
return 'About me!';
});
$page = isset($_GET['page']) ? $_GET['page'] : '/';
$dispatcher = new \PHPLegends\Routes\Dispatcher($page, $_SERVER['REQUEST_METHOD']);
echo $router->dispatch($dispatcher);
Note : I contribute to the development of this last library.
You do not need frameworks to create anything, you only need a framework if you do not have the time or feel that the framework is "good" and meets you, frameworks were created by people just like us, but are usually maintained by communities larger (2 or more people).
In short, any language can do anything that another framework has done.
Now the most important point, routing or routes are not linked to MVC, it's just a feature of some frameworks that combine two different things to summarize the MVC is not a technology , it is a "project organization method" or a design pattern .)
The routes refer to the URLs and sometimes domains that are passed in the form of a variable for PHP, a fairly simple example and no framework using Apache would be this (most servers are Apache):
Create a file named .htaccess
in the folder of your project (in the same folder as your index.php) with the following content:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?uri_path=$1
In the same folder, in index.php do this:
<?php
$uri_path = empty($_GET['uri_path']) ? null : $_GET['uri_path'];
$rotas = array(
'/' => 'pages/home.php', //Este será a index se acessado http://localhost/projeto/
'/sobre' => 'pages/about.php',
'/carrinho' => 'pages/cart.php',
'/admin' => 'pages/admin/home.php', //Pagina para o seu "dashboard"
'/perfil' => 'outro/foo/bar/script_de_perfil.php'
);
$paginaAtual = empty($rotas[$uri_path]) ? null : $rotas[$uri_path];
if ($paginaAtual) {
//Chama a página
include $paginaAtual;
} else {
include 'error/404.php';
}
This is a very simple example as I have already mentioned, to pass arguments like http://localhost/projeto/perfil-{id}
and get id
would require preg_match
, but ae would be another story.
There is no default route, you can create your own pattern, use something similar to known frameworks
It is, roughly speaking, the urls that we will use to access our system. For example, if I want to create a view that allows me to register a user I need to define a route to either get to this view or to move to another view at the end of the registration. Routing allows us to manage how our application will interact, whether to move from one view to another or to pass controller functions.