最为常用的Laravel操作(2)-路由

仁扬仁扬 -
最为常用的Laravel操作(2)-路由
基本路由
// 接收一个 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/
特别申明:本文内容来源网络,版权归原作者所有,如有侵权请立即与我们联系(cy198701067573@163.com),我们将及时处理。

php介绍

PHP即“超文本预处理器”,是一种通用开源脚本语言。PHP是在服务器端执行的脚本语言,与C语言类似,是常用的网站编程语言。PHP独特的语法混合了C、Java、Perl以及 PHP 自创的语法。利于学习,使用广泛,主要适用于Web开发领域。

Tags 标签

lavarelphp

扩展阅读

加个好友,技术交流

1628738909466805.jpg