当前位置:网站首页>Laravel document sorting 2. Route related

Laravel document sorting 2. Route related

2022-06-25 04:19:00 Angry devil

Preface :Laravel Document sorting , Only for record , Nothing else .


1、laravel Framework routing file :
app/Http/routes.php
Ps: The file will be App\Providers\RouteServiceProvider class load

2、 The most basic laravel Route is :
Route::get('/', function () {
    return 'Hello World';
});

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

});

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

Route::delete('foo/bar', function () {
    //
});
Ps: Receive only url And a closure

3、 How to define how to respond to multiple http Routing of actions ?
Route::match(['get', 'post'], '/', function () {
    return 'Hello World';
});
Ps: keyword match

4、 Corresponding multiple http Another way to write the route of actions :
Route::any('foo', function () {
    return 'Hello World';
});

5、 Auxiliary functions generate routes
$url = url('foo');

6、 Configure parameters in the route
Route::get('user/{id}', function ($id) {
    return 'User '.$id;
});

7、 Define any number of parameters in the route
Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
    //
});
Ps:
A、 The parameters of the route will be placed in 「 Curly braces 」 Inside . When running a route , Parameters are passed by routing closed packets .
B、 Routing parameters cannot contain - character . Please underline (_) Replace .

8、 Define optional parameters in the route
Route::get('user/{name?}', function ($name = null) {
    return $name;
});

Route::get('user/{name?}', function ($name = 'John') {
    return $name;
});
Ps: Add... After the parameter name ?

9、 Format of restriction parameter in route
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]+']);
Ps: Use where Method

10、 Rules for globally defining routing parameters :
stay App\Providers\RouteServiceProvider Class boot Method :
/**
 * Define your routing model bindings , Mode filter, etc .
 *
 * @param  \Illuminate\Routing\Router  $router
 * @return void
 */
public function boot(Router $router)
{
    $router->pattern('id', '[0-9]+');

    parent::boot($router);
}

Ps: Note that once the schema is defined , Then all routes must follow this rule .

11、 How to alias a route
Route::get('user/profile', ['as' => 'profile', function () {
    //
}]);、
Ps: as keyword

12、 Specify the route to the corresponding controller
Route::get('user/profile', [
    'as' => 'profile',
    'uses' => '[email protected]'
]);

13、 Another way to define a route :
Route::get('user/profile', '[email protected]')->name('profile');

Ps: call chaining name Method

14、 Set the same prefix for all routes in the group
Route::group(['as' => 'admin::'], function () {
    Route::get('dashboard', ['as' => 'dashboard', function () {
        // Route name is 「admin::dashboard」
    }]);
});

15、 Named route generation urls
$url = route('profile');

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

Ps: Redirection can also be implemented .

16、 Named parameters define parameters , How to generate urls
Route::get('user/{id}/profile', ['as' => 'profile', function ($id) {
    //
}]);

$url = route('profile', ['id' => 1]);
Ps: Give to the route Method .

原网站

版权声明
本文为[Angry devil]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202210535178290.html