Thursday, January 29, 2015

Routing mạnh mẽ, linh hoạt

Đơn giản, linh hoạt và bảo mật hơn với routing.
Định nghĩa rõ ràng phương thức tương tác, các biến, validate CSRF token, validate tham số trên URL ngay ở bước khai báo routing
Route::post('foo/bar', function()
{
    return 'Hello World';
});

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

Route::delete('foo/bar', function()
{
    //
});
Regular Expression Parameter Constraints
Route::get('user/{name}', function($name)
{
    //
})
->where('name', '[A-Za-z]+');

Route::get('user/{id}', function($id)
{
    //
})
->where('id', '[0-9]+');
Passing An Array Of Constraints
Route::get('user/{id}/{name}', function($id, $name)
{
    //
})
->where(['id' => '[0-9]+', 'name' => '[a-z]+'])
Defining Global Patterns
Định nghĩa Global Patterns để applied cho nhiều routing. Ví dụ dưới là định nghĩa biến số nguyên "id"
$router->pattern('id', '[0-9]+');
Route::get('user/{id}', function($id)
{
    // Only called if {id} is numeric.
});
Khai báo tên cho Routing, tiện lợi cho việc generate ra URL. 
Route::get('user/profile', ['as' => 'profile', 'uses' => 'UserController@showProfile']);
$url = route('profile');

$redirect = redirect()->route('profile');
Trên đây là 1 số tính năng thường dùng.
Tham khảo thêm tại đây: http://laravel.com/docs/master/routing

No comments:

Post a Comment