关于lavarel:最为常用的Laravel操作2路由

0次阅读

共计 2905 个字符,预计需要花费 8 分钟才能阅读完成。

根本路由

// 接管一个 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) {//});

如果申请 URLprofile/1, 就会注入一个用户 ID1User 实例, 如果匹配的模型实例在数据库不存在, 会主动生成并返回 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-11,原文链接:https://imlht.com/archives/155/

正文完
 0