Follow me to learn Laravel routing _php instances

Source: Internet
Author: User
Basic Routing

Most of the roads in your app are defined in the app/routes.php file. The simplest laravel route consists of a URI and a closure callback function.

Basic GET Route

Copy the Code code as follows:
Route::get ('/', function ()
{
Return ' Hello world ';
});

Basic POST Routing

Copy the Code code as follows:
Route::p ost (' Foo/bar ', function ()
{
Return ' Hello world ';
});

Register a route that can respond to any HTTP action

Copy the Code code as follows:
Route::any (' foo ', function ()
{
Return ' Hello world ';
});

Only HTTPS-enabled routes

Copy the Code code as follows:
Route::get (' foo ', Array (' HTTPS ', function ()
{
Return ' must is over HTTPS ';
}));

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

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

Route parameters

Copy the Code code as follows:
Route::get (' User/{id} ', function ($id)
{
Return ' User '. $id;
});

Optional routing parameters

Copy the Code code as follows:
Route::get (' user/{name} ', function ($name = null)
{
return $name;
});

Optional route parameters with default values

Copy the Code code as follows:
Route::get (' user/{name} ', function ($name = ' John ')
{
return $name;
});

Routing parameters that are qualified with regular expressions

Copy the 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 a parameter-qualified array

Of course, you can also pass an array containing the parameter qualification as a parameter when necessary:

Copy the Code code as follows:
Route::get (' user/{id}/{name} ', function ($id, $name)
{
//
})
->where (Array (' id ' = ' = ' [0-9]+ ', ' name ' = ' [a-z]+ ')]

Defining the Global Schema

If you want to qualify a route parameter with a specified regular expression at the global scope, you can use the pattern method:

Copy the Code code as follows:
Route::p attern (' id ', ' [0-9]+ ');

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

Access Routing Parameters

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

Copy the Code code as follows:
Route::filter (' foo ', function ()
{
if (route::input (' id ') = = 1)
{
//
}
});

Routing Filters

Route filters provide a convenient way to restrict access to certain features in your application, such as those that require authentication to be accessible. The Laravel framework itself has provided some filters, including auth filters, Auth.basic filters, guest filters, and CSRF filters. These filters are defined in the app/filter.php file.

Define a route filter

Copy the Code code as follows:
Route::filter (' old ', function ()
{
if (Input::get (' age ') < 200)
{
Return redirect::to (' home ');
}
});

If a response is returned from the route filter, then 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 is not executed.

Binding Filters for routes

Copy the Code code as follows:
Route::get (' user ', Array (' before ' = ' old ', function ()
{
Return ' You were over years old! ';
}));

Bind the filter to the controller action

Copy the Code code as follows:
Route::get (' user ', Array (' before ' = ' old ', ' uses ' = ' usercontroller@showprofile '));

Binding multiple filters for routing

Copy the Code code as follows:
Route::get (' user ', Array (' before ' = ' auth|old ', function ()
{
Return ' You were authenticated and over years old! ';
}));

Specifying filter Parameters

Copy the Code code as follows:
Route::filter (' Age ', function ($route, $request, $value)
{
//
});

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

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

Copy the 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 URIs.

Copy the 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 character and will match any combination of characters.

You can also qualify a pattern filter for HTTP actions:

Copy the 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. Since filter classes are parsed through IOC container, you can use dependency injection (Dependency injection) in these filters to achieve better testing capabilities.

Define a filter class

Copy the Code code as follows:
Class Foofilter {

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

}

Registering Filter Classes

Copy the Code code as follows:
Route::filter (' foo ', ' Foofilter ');

Named routes

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

Copy the Code code as follows:
Route::get (' User/profile ', Array (' as ' = ' profile ', function ()
{
//
}));

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

Copy the Code code as follows:
Route::get (' User/profile ', Array (' as ' = ' profile ', ' uses ' = ' usercontroller@showprofile '));

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

Copy the 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 the Code code as follows:
$name = Route::currentroutename ();

Routing groups

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 the 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 Routing

The routing feature in Laravel also supports wildcard domain names, and you can specify wildcard parameters in the domain name:

Registering sub-domain Routing

Copy the Code code as follows:
Route::group (Array (' domain ' = ' = ' {account}.myapp.com '), function ()
{

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

});

Route prefixes

You can set the prefix for group routing through the prefix property:

To set a prefix for a routing group

Copy the 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, instead of just injecting a user ID, you can inject the entire model instance into the route that matches the user ID. First, use the Route::model method to specify the model to be injected:

The reference to a model

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

Copy the Code code as follows:
Route::get (' Profile/{user} ', function (user $user)
{
//
});

Since we have bound the {user} parameter to the user model, we 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, the 404 error 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 the Code code as follows:
Route::model (' user ', ' user ', function ()
{
throw new Notfoundexception;
});

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

Copy the Code code as follows:
Route::bind (' User ', function ($value, $route)
{
Return User::where (' name ', $value)->first ();
});

Throw a 404 error

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

Copy the Code code as follows:
App::abort (404);

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

For more information on handling 404 exceptions and customizing response when an error occurs, you can view the error document.

Controller routing

Laravel not only provides the ability to handle routing with the closure function, it can also be routed to the controller, and even supports 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.