Home  >  Article  >  php教程  >  Laravel framework routing configuration summary and setting tips

Laravel framework routing configuration summary and setting tips

高洛峰
高洛峰Original
2016-12-27 11:12:491453browse

Basic Routing

The vast majority of your application's routes will be defined in the app/routes.php file. The simplest route in Laravel consists of a URI and a closure call.

Basic GET route

<span style="font-size: 16px;">Route::get(&#39;/&#39;, function()<br/>{<br/>return &#39;Hello World&#39;;<br/>});<br/></span>

Basic POST route

<span style="font-size: 16px;">Route::post(&#39;foo/bar&#39;, function()<br/>{<br/>return &#39;Hello World&#39;;<br/>});<br/></span>

Register a route to respond to all HTTP methods

<span style="font-size: 16px;">Route::any(&#39;foo&#39;, function()<br/>{<br/>   return &#39;Hello World&#39;;<br/>});<br/></span>

Force a route to be accessed via HTTPS

<span style="font-size: 16px;">Route::get(&#39;foo&#39;, array(&#39;https&#39;, function()<br/>{<br/>    return &#39;Must be over HTTPS&#39;;<br/>}));<br/></span>

Often you need to generate URLs based on a route. You can do this by using URL:: to method:

<span style="font-size: 16px;">$url = URL::to(&#39;foo&#39;);<br/></span>

routing parameters

<span style="font-size: 16px;">Route::get(&#39;user/{id}&#39;, function($id)<br/>{<br/>return &#39;User &#39;.$id;<br/>});<br/></span>

optional routing parameters

<span style="font-size: 16px;">Route::get(&#39;user/{name?}&#39;, function($name = null)<br/>{<br/>return $name;<br/>});<br/></span>

band Optional route parameters with default values

<span style="font-size: 16px;">Route::get(&#39;user/{name?}&#39;, function($name = &#39;John&#39;)<br/>{<br/>return $name;<br/>});<br/></span>

Routes with regular expression constraints

<span style="font-size: 16px;">Route::get(&#39;user/{name}&#39;, function($name)<br/>{<br/>//<br/>})<br/>->where(&#39;name&#39;, &#39;[A-Za-z]+&#39;);<br/>Route::get(&#39;user/{id}&#39;, function($id)<br/>{<br/>//<br/>})<br/>->where(&#39;id&#39;, &#39;[0-9]+&#39;);<br/></span>

Route filters

Route filters provide a simple way to restrict access to specific routes, which is useful when you need to create zones for your site that require authentication. The Laravel framework contains some routing filters, such as auth filter, auth.basic filter, guest filter, and csrf filter. They are stored in the app/filters.php file.

Define a routing filter

Route::filter(&#39;old&#39;, function()
{
if (Input::get(&#39;age&#39;) < 200)
{
return Redirect::to(&#39;home&#39;);
}
});

If a response is returned from a routing filter, this response is considered a response to this request, and the routing will not be executed. Any information about this routing The after filter will also be de-executed.

Route::get(&#39;user&#39;, array(&#39;before&#39; => &#39;old&#39;, function()
{
return &#39;You are over 200 years old!&#39;;
}));

Specify a route filter for a route

Route::get(&#39;user&#39;, array(&#39;before&#39; => &#39;auth|old&#39;, function()
{
return &#39;You are authenticated and over 200 years old!&#39;;
}));

Specify route filter parameters

Route::filter(&#39;age&#39;, function($route, $request, $value)
{
//
});
Route::get(&#39;user&#39;, array(&#39;before&#39; => &#39;age:200&#39;, function()
{
return &#39;Hello World&#39;;
}));

When the route filter receives the response $response as the third parameter:

Route::filter(&#39;log&#39;, function($route, $request, $response, $value)
{
//
});

Patterns for Basic Route Filters

You may want to specify a filter for a set of routes based on a URI.

Route::filter(&#39;admin&#39;, function()
{
//
});
Route::when(&#39;admin/*&#39;, &#39;admin&#39;);

In the above example, the admin filter will be applied with all routes starting with admin/. The asterisk acts as a wildcard character and will match all character combinations.

You can also constrain a pattern filter by specifying the HTTP method:

Route::when(&#39;admin/*&#39;, &#39;admin&#39;, array(&#39;post&#39;));

Filter class

For advanced filters, you can use a class instead of a closure function. Because the filter class is an IoC container that lives outside the application, you can use dependency injection in the filter, making it easier to test.

Define a filter class

class FooFilter {
public function filter()
{
// Filter logic...
}
}

Register a class-based filter

Route::filter(&#39;foo&#39;, &#39;FooFilter&#39;);

Named routes

Named routes make it easier to generate jumps or Routes are specified when specifying URLs. You can give a route a name like this:

Route::get(&#39;user/profile&#39;, array(&#39;as&#39; => &#39;profile&#39;, function()
{
//
}));

You can also give a controller method a route name:

  Route::get(&#39;user/profile&#39;, array(&#39;as&#39; => &#39;profile&#39;, &#39;uses&#39; => 
&#39;UserController@showProfile&#39;));

Now you use the route name when generating URLs or redirects. :

$url = URL::route(&#39;profile&#39;);
$redirect = Redirect::route(&#39;profile&#39;);

You can use the currentRouteName method to get the name of a route:

$name = Route::currentRouteName();

Route Group

Sometimes you may want to apply a filter to a group of routes. You don't need to specify filters for each route, you can use route groups:

Route::group(array(&#39;before&#39; => &#39;auth&#39;), function()
{
Route::get(&#39;/&#39;, function()
{
// Has Auth Filter
});
Route::get(&#39;user/profile&#39;, function()
{
// Has Auth Filter
});
});

Subdomain routing

Laravel routing can also handle wildcard subdomains and get wildcard parameters from the domain name:

Register subdomain routing

Route::group(array(&#39;domain&#39; => &#39;{account}.myapp.com&#39;), function()
{
Route::get(&#39;user/{id}&#39;, function($account, $id)
{
//
});
});

Route prefix

A group of routes can add a prefix to the routing group by using the prefix option in the attribute array:

is Add the prefix

Route::group(array(&#39;prefix&#39; => &#39;admin&#39;), function()
{
Route::get(&#39;user&#39;, function()
{
//
});
});

to the route group *Route model binding

Model binding provides a simple way to inject models into routes. For example, instead of just injecting a user's ID, you can inject an entire user model instance based on a specified ID. First use the Route::model method to specify the required model:

Bind a variable to the model

Route::model(&#39;user&#39;, &#39;User&#39;);

Then, define a route containing the {user} parameter:

Route::get(&#39;profile/{user}&#39;, function(User $user)
{
//
});

Since we have bound the {user} parameter to the User model, a User instance will be injected into the route. So, for example, a request for profile/1 will inject a User instance with ID 1.

Note: If this model instance is not found in the database, a 404 error will be raised.

If you want to specify your own behavior that is not found, you can pass a closure as the third parameter to the model method:

Route::model(&#39;user&#39;, &#39;User&#39;, function()
{
throw new NotFoundException;
});

Sometimes you want to use your own method to handle routing Parameters, you can use the Route::bind method:

Route::bind(&#39;user&#39;, function($value, $route)
{
return User::where(&#39;name&#39;, $value)->first();
});

Trigger 404 error

There are two methods to manually trigger a 404 error in routing. First, you can use the App::abort method:

App::abort(404);

Second, you can throw an instance of SymfonyComponentHttpKernelExceptionNotFoundHttpException.

More information about handling 404 exceptions and using custom responses for these errors can be found in the Errors chapter.

Routing to Controllers

Laravel not only allows you to route to closures, but also to controller classes, and even allows you to create resource controllers.

More information please Access controller documentation.

For more articles related to Laravel framework routing configuration summary and setting tips, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn