关于Laravel的二、三事(1)简单的路由

2 篇文章 0 订阅

用于记录学习Laravel的笔记,今天学习的是关于路由的简单知识

基本路由

最简单的路由,由接收一个url和一个闭包的形式展示:

Route::get('/', function () {
    return 'Hello World';
});

Route::post('foo/bar', function () {
    return 'Hello World';
});

Route::put('foo/bar', function () {
    //
});

Route::delete('foo/bar', function () {
    //
});
对同一个路由,我们可以设置使其接受多个动词:

Route::match(['get', 'post'], '/', function () {
    return 'Hello World';
});

其中如果使用了any设置,那么所有动词均能被接受:

Route::any('foo', function () {
    return 'Hello World';
});


路由参数

当你的路由需要传入参数的时候,Laravel提供了传入参数的路由方式,当只有一个参数时:

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]+']);
除了直接在路由之后直接进行正则校验之外,Laravel还提供了设置全局参数校验的方式,只需要在RouteServiceProvider::boot()方法中添加对应的规则即可:

public function boot(Router $router)
{
    $router->pattern('id', '[0-9]+');

    parent::boot($router);
}

所有的参数名为id的参数都会进行RouteServiceProvider::boot()中的校验。

RouteServiceProvider::class主要进行两件事的操作:1.绑定路由参数校验;2.为当前请求设置路由。


Laravel还提供了命名路由的功能,除了通过给出的匹配规则外还可以为路由命名:

Route::get('user/profile', ['as' => 'profile', function () {
    //
}]);
或者

Route::get('user/profile', [
    'as' => 'profile', 'uses' => 'UserController@showProfile'
]);
或者

Route::get('user/profile', 'UserController@showProfile')->name('profile');
绑定之后访问user/profile和访问profile的内容将会是一致的。


当你给一个路由命名之后,你可能会在视图或者其他的路由或者控制器使用到当前路由,便可以通过:

$url = route('profile');

$redirect = redirect()->route('profile');

进行重定向。

对于带有参数的路由,则可以通过:

Route::get('user/{id}/profile', ['as' => 'profile', function ($id) {
    //
}]);

$url = route('profile', ['id' => 1]);

这样来生成对应的URL。


路由组

对于一个路由组,可以使用键值对数组来声明一系列按照顺序执行的中间件,并且适用于组内的所有路由:

Route::group(['middleware' => 'auth'], function () {
    Route::get('/', function ()    {
        // Uses Auth Middleware
    });

    Route::get('user/profile', function () {
        // Uses Auth Middleware
    });
});

同时可以使用键值对数组来声明组内路由遵循的命名空间:

Route::group(['namespace' => 'Admin'], function()
{
    // Controllers Within The "App\Http\Controllers\Admin" Namespace

    Route::group(['namespace' => 'User'], function()
    {
        // Controllers Within The "App\Http\Controllers\Admin\User" Namespace
    });
});


这里有一个注意点:

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to the controller routes in your routes file.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    public function boot(Router $router)
    {
        //

        parent::boot($router);
    }

    /**
     * Define the routes for the application.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    public function map(Router $router)
    {
        $router->group(['namespace' => $this->namespace], function ($router) {
            require app_path('Http/routes.php');
        });
    }
}

App\Providers\RouteServiceProvider::class的源码中显而易见的包含了参数$namespace,并且在映射路由的时候已经包含了Http/routes.php,也就是说,如果你的路由的命名空间都是App\Http\Controllers,那么就不需要设置路由命名空间参数了,对于命名空间与之不同的路由再进行设置即可。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值