Follow me to learn the Laravel Route _php instance

Source: Internet
Author: User
Tags auth closure

Basic routing

Most of the paths in the application are defined in the app/routes.php file. The simplest Laravel routing consists of a URI and a closed-pack callback function.

Basic Get route

Copy Code code as follows:

Route::get ('/', function ()
{
Return to ' Hello world ';
});

Basic POST Route

Copy Code code as follows:

Route::p ost (' Foo/bar ', function ()
{
Return to ' Hello world ';
});

Registers a route that can respond to any HTTP action

Copy Code code as follows:

Route::any (' foo ', function ()
{
Return to ' Hello world ';
});

Only HTTPS-enabled routes

Copy Code code as follows:

Route::get (' foo ', Array (' HTTPS ', function ()
{
Return ' must is over HTTPS ';
}));

In actual development, it is often necessary to generate url,url::to methods based on routing to meet this requirement:

$url = url::to (' foo ');

Routing parameters

Copy Code code as follows:

Route::get (' User/{id} ', function ($id)
{
Return ' User '. $id;
});

Optional route parameters

Copy Code code as follows:

Route::get (' User/{name?} ', function ($name = null)
{
return $name;
});

Optional route parameters with default values

Copy Code code as follows:

Route::get (' User/{name?} ', function ($name = ' John ')
{
return $name;
});

Route parameters qualified with a regular expression

Copy Code code as follows:

Route::get (' user/{name} ', function ($name)
{
//
})
->where (' name ', ' [a-za-z]+ ');

Route::get (' User/{id} ', function ($id)
{
//
})
->where (' id ', ' [0-9]+ ');

Passing an array of parameter qualifications

Of course, you can also pass a parameter-qualified array as an argument when necessary:

Copy Code code as follows:

Route::get (' user/{id}/{name} ', function ($id, $name)
{
//
})
->where (Array (' ID ' => ' [0-9]+ ', ' name ' => ' [a-z]+ ')]

Define Global Mode

If you want to qualify routing parameters in a global scope with a specified regular expression, you can use the pattern method:

Copy Code code as follows:

Route::p attern (' id ', ' [0-9]+ ');

Route::get (' User/{id} ', function ($id)
{
Only called if {ID} is numeric.
});

Accessing route parameters

If you want to access routing parameters outside the routing scope, you can use the Route::input method:

Copy Code code as follows:

Route::filter (' foo ', function ()
{
if (route::input (' id ') = = 1)
{
//
}
});

Routing Filters

Routing filters provide a convenient way to restrict access to certain features in your application, such as those that require authentication to access. The Laravel framework itself already provides a number of filters, including auth filters, Auth.basic filters, guest filters, and CSRF filters. These filters are defined in the app/filter.php file.

Define a routing filter

Copy Code code as follows:

Route::filter (' old ', function ()
{
if (Input::get (' age ') < 200)
{
Return redirect::to (' home ');
}
});

If a response is returned from the routing filter, the response will be considered to correspond to this request, the route will not be executed, and all code defined in this route after this filter will not be executed.

Binding Filters for routing

Copy Code code as follows:

Route::get (' user ', Array (' Before ' => ' old ', function ()
{
Return ' to are over years old! ';
}));

To bind a filter to a controller action

Copy Code code as follows:

Route::get (' user ', Array (' Before ' => ' old ', ' uses ' => ' usercontroller@showprofile '));

To bind multiple filters for a route

Copy Code code as follows:

Route::get (' user ', Array (' Before ' => ' Auth|old ', function ()
{
Return ' to are authenticated and over years old! ';
}));

Specify Filter Parameters

Copy Code code as follows:

Route::filter (' Age ', function ($route, $request, $value)
{
//
});

Route::get (' user ', Array (' Before ' => ' age:200 ', function ()
{
Return to ' Hello world ';
}));

All subsequent filters will receive the $response as the third parameter:

Copy Code code as follows:

Route::filter (' Log ', function ($route, $request, $response, $value)
{
//
});

Pattern-based filters

You can also refer to specifying a filter for a set of routes for a URI.

Copy Code code as follows:

Route::filter (' admin ', function ()
{
//
});

Route::when (' admin/* ', ' admin ');

In the above case, the admin filter will be applied to all routes beginning with admin/. The asterisk is a wildcard and will match any combination of multiple characters.

You can also qualify mode filters for HTTP actions:

Copy Code code as follows:

Route::when (' admin/* ', ' admin ', array (' post '));

Filter class

In advanced usage of filters, you can also use classes to override closure functions. Because filter classes are parsed through IOC container, you can use the method of dependency injection (dependency injection) in these filters to achieve better testing capabilities.

Define a filter class

Copy Code code as follows:

Class Foofilter {

Public Function Filter ()
{
Filter logic ...
}

}

Register Filter Class

Copy Code code as follows:

Route::filter (' foo ', ' Foofilter ');

Named routes

Using named routes is more convenient when redirecting and generating URLs. You can specify a name for the route, as follows:

Copy Code code as follows:

Route::get (' User/profile ', Array (' as ' => ' profile '), function ()
{
//
}));

You can also specify a route name for the controller action:

Copy Code code as follows:

Route::get (' User/profile ', Array (' as ' => ' profile ', ' uses ' => ' usercontroller@showprofile '));

Now you can use the routing name to create URLs and redirects:

Copy Code code as follows:

$url = Url::route (' profile ');

$redirect = Redirect::route (' profile ');

You can use the Currentroutename method to get the currently running route name:

Copy Code code as follows:

$name = Route::currentroutename ();

Routing Group

Sometimes you may need to apply filters for a set of routes. By using routing groups, you can avoid specifying filters for each route individually:

Copy Code code as follows:

Route::group (Array (' Before ' => ' auth '), function ()
{
Route::get ('/', function ()
{
Has Auth Filter
});

Route::get (' User/profile ', function ()
{
Has Auth Filter
});
});

Sub-domain name routing

The routing function in Laravel also supports the wildcard subcode domain name, where you can specify wildcard parameters in the domain name:

Registering child domain name routing

Copy Code code as follows:

Route::group (Array (' domain ' => ' {account}.myapp.com '), function ()
{

Route::get (' User/{id} ', function ($account, $id)
{
//
});

});

Route prefix

You can set a prefix for a group route by using the prefix property:

To set a prefix for a routing group

Copy Code code as follows:

Route::group (Array (' prefix ' => ' admin '), function ()
{

Route::get (' User ', function ()
{
//
});

});

Routing and Model binding

Model binding provides a convenient way to inject model instances into a route. For example, you can inject an entire model instance of a matching user ID into a route instead of just injecting a user ID. First, use the Route::model method to specify the model to be injected:

Will participate in a model

Route::model (' user ', ' user ');
Then, define a route that contains the {user} parameter:

Copy Code code as follows:

Route::get (' Profile/{user} ', function (user $user)
{
//
});

Because we have bound the {user} parameter to the user model, you can inject a user instance into the route. For example, access to PROFILE/1 will inject a user instance with ID 1 into the route.

Note: If the corresponding model instance cannot be matched in the database, 404 errors will be thrown.
If you want to customize the "not found" behavior, you can pass a closure function as the third parameter of the model method:

Copy Code code as follows:

Route::model (' user ', ' user ', function ()
{
throw new Notfoundexception;
});

If you want to implement routing parameter parsing yourself, simply use the Route::bind method:

Copy Code code as follows:

Route::bind (' User ', function ($value, $route)
{
Return User::where (' name ', $value)->first ();
});

Throw 404 Error

There are two ways to manually trigger the 404 error from the route. First, you can use the App::abort method:

Copy Code code as follows:

App::abort (404);

Second, you can throw symfony\component\httpkernel\exception\notfoundhttpexception exceptions.

For more information about handling 404 exceptions and customizing response when errors occur, you can view the error document.

Controller routing

Laravel not only provides the ability to handle routing by using closure functions, but it can also be routed to controllers and even support the creation of resource controllers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.