What does the term "routing" mean in the context of MVC architecture?

Asked

Viewed 665 times

5

I’m beginning in the study of this architecture and came across this term : routing.

Edit

I need some framework to develop based on this architecture ?

  • 2

    I know it’s another technology, but doesn’t that answer anymore? It works the same. http://answall.com/q/162142/101

  • I know very little about it to say if you answer, at first try to understand something for the first time in another language already makes it a little difficult for me, eventually if someone can post something in the context of php, for me it would be easier to understand...

  • Just a note: Routing or routes is not linked to MVC, it is just a feature of some frameworks that combine two different things, to summarize MVC is not a technology, it would be a "method of organizing the project" (Design or English standard Pattern design).

3 answers

8


"Routing" translating is "Roteamento".

From what I understand of MVC, the term Routing is related to the route control/definition of an application.

Those routes are usually used to define an action that will be executed when a given url is accessed.

Example: redirects, response in json or html.

Example in Frameworks

It is very common to use routes in PHP frameworks that use the MVC standard.

Let me give you an example of defining routes in the framework called Laravel.

Example:

Route::get('/', 'HomeController@getIndex');

Route::get('/about', 'HomeController@getAbout');

Basically, in the statement above, you are setting two routes. Each one performs a different action, for each path different (path is the part of the url that is after /).

That is, when someone accesses the url meusite.com.br the method getIndex of HomeController will be invoked. However, if you access meusite.com.br/about, will be invoked getAbout.

Still in relation to the term Routing, I could not help but mention that the frameworks Symfony and Laravel use this term Routing internally in their namespaces.

These frameworks are separated by several libraries. And in one of these, there is a call Routing. This is because the developers of these two frameworks had the idea to create the entire separate routing structure in a specific library.

It is important to mention that the functionalities attributed to routing of the above mentioned libraries go beyond a definition of an action for each url. In the routing of these frameworks, other responsibilities are assigned to this library. They are:

  • Setting filters. A route can only be accessed according to one condition. For example, an authenticated user can access a route a, but an undocumented cannot.

  • Route collection. A class responsible for recording all route information.

  • Error handling. If the accessed url represents an undefined path, a default action must be called.

  • Verification of the verb of the request. In this case, it is a question of knowing if the route accepts requests of type POST, GET, PUT, PATCH, DELETE, etc. It is useful to do this check for Restful applications.

Routing libraries

Library of Routing of Symfony and Laravel

The libraries cited above are currently on Github. If you want to take a look to see how they were organized, take a look at them:

Library Routes of the Phplegends

The library PHPLegends/Routes has a very simple way for you to create a route system, without dependency on a framework.

Example (similar to the 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 cited library.

  • No bro forgets Asp.net, it’s php....

  • You have to use framework ?

  • You can put an example of php usage without framework ?

  • @Magichat to solving a Mysql BO. I’ll improve it for you

  • In good man’s time......

  • @Wallacemaxters only one question, in the case of MVC, the question of filters of a route should not be directly related to the model? For example, if the user is not logged in he cannot access the route.. who analyzes this is the right model? Or you have to "write" that on the route too? Sorry to return to the subject.

  • +1 for the answer.. very good!

Show 2 more comments

8

You don’t need frameworks to create anything, you only need a framework if you don’t have the time or think the framework is "good" and suits you, frameworks were created by people just like us, but are usually maintained by larger communities (2 or more people).

Summarizing in any language it is possible to do anything that another framework has done.

Now the most important point, routing or routes are not linked to MVC, it is just a feature of some frameworks that combine two different things, to summarize the MVC is not a technology, it is "method of organization of the project" (Design pattern or English Pattern design).

The routes refer to Urls and sometimes domains that are passed in variable form to PHP, an example fairly simple and no framework using Apache would be this (most servers are Apache):

  1. Create a file called .htaccess in your project folder (in the same folder as your index.php) with the following contents:

    RewriteEngine On
    
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?uri_path=$1
    
  2. 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 on arguments such as http://localhost/projeto/perfil-{id} and catch the id it would be necessary preg_match, but that would be another story.

There is no standard for doing the routes, you can create your own pattern, use something similar to the known frameworks

  • 1

    Orra I like this explanation... I was able to capture... Vlw man...

  • Although the question was specifically about MVC, it was important to mention that it is possible to create a route organization, without needing frameworks, library and the like. + 1

  • @Wallacemaxters but I talked about MVC, this is not technology and it’s not connected to routes, what happens is that frameworks use the pattner MVC design to organize the project and transfer the routes to access one or more controllers, but at the end that defines whether MVC is the same developer, you can use Laravel and still not be doing MVC :)

  • Ah, it’s true, I falter!

0

It’s roughly the urls we’re going to 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 both to get to this view and to move to another view when finishing 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.

  • Can you give an example of how this would be done routing, some code...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.