最为常用的Laravel操作(2)-路由
导读:基本路由// 接收一个 URI 和一个闭包 Route::get('hello', function ( { return 'Hello, Laravel'; } ; // 支持的路由方法 Route::get($uri, $c...
基本路由
// 接收一个 URI 和一个闭包
Route::get('hello', function () {
return 'Hello, Laravel';
}
);
// 支持的路由方法
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
// 支持多个路由方法
Route::match(['get', 'post'], '/', function () {
//
}
);
// 注册所有路由方法
Route::any('foo', function () {
//
}
);
路由参数
- 使用花括号包裹
- 路由参数不能包含 - 字符, 需要的话可以使用 _ 替代
// 捕获用户 ID
Route::get('user/{
id}
', function ($id) {
return 'User '.$id;
}
);
// 捕获多个参数
Route::get('posts/{
post}
/comments/{
comment}
', function ($postId, $commentId) {
//
}
);
// 可选参数
Route::get('user/{
name?}
', function ($name = null) {
return $name;
}
);
Route::get('user/{
name?}
', function ($name = 'John') {
return $name;
}
);
// 正则约束
Route::get('user/{
name}
', function ($name) {
//
}
)->
where('name', '[A-Za-z]+');
Route::get('user/{
id}
', function ($id) {
//
}
)->
where('id', '[0-9]+');
Route::get('user/{
id}
/{
name}
', function ($id, $name) {
//
}
)->
where(['id' =>
'[0-9]+', 'name' =>
'[a-z]+']);
命名路由
// 为路由闭包指定名称
Route::get('user/profile', function () {
//
}
)->
name('profile');
// 为控制器操作指定名称
Route::get('user/profile', 'UserController@showProfile')->
name('profile');
// 使用命名路由生成 URL: 不带参数
$url = route('profile');
return redirect()->
route('profile');
// 使用命名路由生成 URL: 附带参数
Route::get('user/{
id}
/profile', function ($id) {
//
}
)->
name('profile');
$url = route('profile', ['id' =>
1]);
路由群组
中间件
Route::group(['middleware' =>
'auth'], function () {
Route::get('/', function () {
// 使用 Auth 中间件
}
);
Route::get('user/profile', function () {
// 使用 Auth 中间件
}
);
}
);
命名空间
Route::group(['namespace' =>
'Admin'], function(){
// 控制器在 "App\Http\Controllers\Admin" 命名空间下
}
);
子域名路由
Route::group(['domain' =>
'{
account}
.myapp.com'], function () {
Route::get('user/{
id}
', function ($account, $id) {
//
}
);
}
);
路由前缀
Route::group(['prefix' =>
'admin'], function () {
Route::get('users', function () {
// 匹配 "/admin/users" URL
}
);
}
);
表单方法伪造
form action="/foo/bar" method="POST">
input type="hidden" name="_method" value="PUT">
input type="hidden" name="_token" value="{
{
csrf_token() }
}
">
/form>
或使用辅助函数 method_field
:
{
{
method_field('PUT') }
}
访问当前路由
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
路由缓存
# 添加路由缓存
php artisan route:cache
# 移除路由缓存
php artisan route:clear
路由模型绑定
隐式绑定
// {
user}
与 $user 绑定, 如果数据库中找不到对应的模型实例, 会自动生成 HTTP 404 响应
Route::get('api/users/{
user}
', function (App\User $user) {
return $user->
email;
}
);
// 自定义键名: 重写模型 getRouteKeyName 方法
/**
* Get the route key for the model.
*
* @return string
*/
public function getRouteKeyName()
{
return 'slug';
}
显式绑定
要注册显式绑定, 需要使用路由的 model
方法来为给定参数指定绑定类. 应该在 RouteServiceProvider
类的 boot
方法中定义模型绑定:
public function boot()
{
parent::boot();
Route::model('user', App\User::class);
}
定义一个包含 {
user}
参数的路由:
$router->
get('profile/{
user}
', function(App\User $user) {
//
}
);
如果请求 URL
是 profile/1
, 就会注入一个用户 ID
为 1
的 User
实例, 如果匹配的模型实例在数据库不存在, 会自动生成并返回 HTTP 404
响应.
自定义解析逻辑
如果你想要使用自定义的解析逻辑, 需要使用 Route::bind
方法, 传递到 bind
方法的闭包会获取到 URI
请求参数中的值, 并且返回你想要在该路由中注入的类实例:
public function boot()
{
parent::boot();
Route::bind('user', function($value) {
return App\User::where('name', $value)->
first();
}
);
}
文章来源于本人博客,发布于 2018-06-02,原文链接:https://imlht.com/archives/155/
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: 最为常用的Laravel操作(2)-路由
本文地址: https://pptw.com/jishu/500902.html