路由相关

本文详细介绍了Laravel框架中的路由配置方式,包括基本的GET、POST请求处理,以及更复杂的匹配多种HTTP方法、参数约束等功能,并展示了如何使用命名路由、中间件组等高级特性。

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';});

 

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

 

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', ['as' => 'profile', function () {
    //}]);
Route::get('user/profile', [
    'as' => 'profile', 'uses' => 'UserController@showProfile']);
Route::get('user/profile', 'UserController@showProfile')->name('profile');

 

Route::group(['as' => 'admin::'], function () {
    Route::get('dashboard', ['as' => 'dashboard', function () {
        // Route named "admin::dashboard"    }]);});
$url = route('profile');$redirect = redirect()->route('profile');

 

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

 

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    });});
Route::group(['domain' => '{account}.myapp.com'], function () {
    Route::get('user/{id}', function ($account, $id) {
        //    });});

 

Route::group(['prefix' => 'admin'], function () {
    Route::get('users', function ()    {
        // Matches The "/admin/users" URL    });});
Route::group(['prefix' => 'accounts/{account_id}'], function () {
    Route::get('detail', function ($account_id)    {
        // Matches The accounts/{account_id}/detail URL    });});
abort(404);

转载于:https://my.oschina.net/sharesuiyue/blog/788979

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值