# routing

[[toc]]

Routing refers to accessing through a specified URI path, without the need to beautify the link like traditional access to static file paths.

All routes should be added in closures in bootstrap.php , like so:

use App \ S ervices\ H ook ; 

return function () {
    Hook::addRoute( function ( $ routes ) {
        // register routes here 
}) ; 
} ;

For more detailed usage of routing, please refer to the HTTP Routing (opens new window) section of the Laravel documentation.

# the easiest route

You can register routes for different HTTP actions according to the needs of the plug-in. What I want to explain here is that even if the URI of the request is the same, if the HTTP action is different, it will indicate a different route, like this:

Hook::addRoute( function ( $ routes ) {
    $routes- >get( 'foo/bar' , 'FoobarController@showPage' ) ;
    $routes- >post( 'foo/bar' , 'FoobarController@foobar' ) ; 
}) ;

Routes for multiple different HTTP actions can be registered using the match method:

Hook::addRoute( function ( $ routes ) {
    $routes -> match ([ 'get' , 'post' ] , 'foo/bar' , 'FoobarController@doSomething' ) ; 
}) ;

You can also use the anymethod to respond to all HTTP actions:

Hook::addRoute( function ( $ routes ) {
    $routes- >any( 'foo/bar' , 'FoobarController@doSomething' ) ; 
}) ;

From Blessing Skin v4, you can use viewand redirectmethods. The second parameter of the view method is the view you want to point to (so you don't have to define a controller method just to display a view); andthe second parameter of the redirect method is the one you want to redirectTarget.

In order to ensure that Laravel can cache the route, it is recommended that you specify it as a string to the controller instead of using a closure (because Laravel does not cache closed routes). The following is the opposite teaching material:

Hook::addRoute( function ( $ routes ) {
    $routes- >any( 'foo/bar' , function () {
        // not recommended 
}) ; 
}) ;

In the route registration, the controller is usually specified like 'FoobarController@doSomething'. Among them, FoobarControlleris the class name corresponding to the controller, and doSomethingis the public method name in FoobarController.

Since plugin code always has a different namespace than the skin station program, you must specify the namespace of the class unless you use routing groups. (About the routing group will be introduced later)

# get parameters from route

You can extract parameters from URI like:

Hook::addRoute( function ( $ routes ) {
    $routes ->any( 'player/{skin_id}/{cape_id?}' , 'FoobarController@doSomething' ) ; 
}) ;

?after the parameter name like {cape_id?}means that the parameter is optional.

And in the controller, you can extract the parameters like below. Don't forget to add a default value to the optional parameter.

public function doSomething( $skin_id , $cape_id = null ) 
{
    // 
}

# routing group

Sometimes you may want to share properties of several routes, in which case you can use route groups. Here is an example of a simple routing group:

Hook::addRoute(function ($routes) {
    $routes->group([
        'middleware' => ['web'],
        'prefix'     => 'hello',
        'namespace'  => 'Example\Foo\Bar'
    ], function ($route) {
        $route->get('world', 'ExampleController@example');
    });
});

The groupmethod receives two parameters. The first parameter is the attribute of the routing group, which is an associative array, and the attributes in it do not have to be limited to the above example; the second parameter is a closure, and the usage in the closure is the same as the routing usage described above.

Here is a brief introduction to some attributes commonly used in routing groups.

  • middleware

This indicates the middleware to be used by the routing group. The middleware will be introduced below. Its value can be either a string or an array of strings (if you want to use multiple middleware at the same time).

  • prefix

Route group prefix. The prefix in the example is hello, then it will respond to routes starting with hello , for example it will respond to ``/hello/world.

  • namespace

Namespaces. Specifies the namespace of the controller within the routing group that will receive the response. Taking the above example as an example, the namespace of ExampleController is specified as ``Example\Foo\Bar, and there is no need to add a namespace for the controller corresponding to each route.

# middleware

From a strict point of view, middleware and routing are two concepts, but considering that the development of plug-ins almost does not need to define middleware, "middleware" is briefly introduced here.

If you're interested in "middleware", you can read the HTTP Middleware (opens new window) section of the Laravel 5.1 documentation.

Here, middleware can filter user requests. For example, when a visitor who is not logged in tries to access the user center, the authmiddleware will check whether the visitor is logged in. If he is logged in, he will be allowed to continue to visit the user center, otherwise he will jump to the login interface.

Blessing Skin provides the following middleware available:

# web

This middleware mainly performs some basic checks such as Cookies, etc. Unless it is for API, it is recommended to use this middleware for general requests.

# auth

This middleware can check if the visitor is logged in or not.

# role

WARNING

This middleware is only available in Blessing Skin v5.0.0 or later.

Used to check the permissions of the current user. This middleware accepts a required parameter, the target permission level. For example, if a page requires administrator privileges, you can write:

R oute::middleware( 'role:admin' )-> //...

If a super administrator is required, it is:

R oute::middleware( 'role:super-admin' )-> //...

# verified

WARNING

This middleware is only available in Blessing Skin v3.5.0 or later.

This middleware checks if the current user's email address is verified.

# usage

You can use the middleware mentioned above in a route group. If it's not a route group, you can use middleware like this:

Hook::addRoute( function ( $ routes ) {
    $routes- >get( 'foo/bar' , 'FoobarController@showPage' )->middleware([ 'web' , 'auth' ]) ; 
}) ;

middlewaremethod are also strings or string arrays.