Dorokhov.codes
04. Routes
Methods
For web: routes/web.php
:
Route::get('/', function () {
return view('welcome');
});
Route::post('/send-email', function () {
return view('contact-us');
});
All available router methods:
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
Several methods at once:
Route::match(['get', 'post'], '/about', function () {
return view('welcome');
});
Any method:
Route::any('/about', function () {
return view('welcome');
});
Without a closure
Route::view('/hello', 'about-us', ['name' => 'Andrew']);
Names for routes
Route::get('/', function () {
return view('welcome');
})->name('home');
No we can use it in the route()
function:
{{ route('home') }}
Redirect
Route::redirect('/about', '/contact'); // default status = 302
## Permanent redirect:
Route::redirect('/about', '/contact', 301); // default status = 301
Route:permanentRedirect('/about', '/contact');
Parameters
Parameters are positioned (we can name them like we want, only the order matters).
Route::get('/post/{id}', function($id) {
return "Post $id";
});
Route::get('/post/{id}/{slug}', function($id, $slug) {
return "Post $id";
});
Make a parameter optional:
Route::get('/post/{id}/{slug?}', function($id, $slug = null) {
return "Post $id";
});
Filters:
Route::get('/post/{id}/{slug}', function($id, $slug) {
return "Post $id";
})->where('id', '[0-9]+');
Route::get('/post/{id}/{slug}', function($id, $slug) {
return "Post $id";
})->where([
'id' => '[0-9]+',
'slug' => '[A-Za-z0-9-]+',
]);
Global filters (in app/Providers/RouteServiceProvider.php
):
public function boot() {
Route::pattern('id', '[0-9]+');
Route::pattern('slug','[A-Za-z0-9-]+');
...
Chaning how a parameter named in a resource:
Route::resource('/posts', 'PostController', [
'parameters' => ['posts' => 'id'],
]);
Grouping URLs
Route::prefix('admin')->group(function () {
Route::get('/posts', function () {
return 'Posts List';
});
Route::get('/post/create', function () {
return 'Post Create';
});
Route::get('/post/{id}/edit', function ($id) {
return "Edit Post $id";
});
});
Render a view
Function view()
looks for a view file in resources/views/
, for example:
resources/views/welcome.blade.php
Passing variables:
Route::get('/', function () {
return view('welcome', [
'name' => 'Andrew',
]);
});
Route::get('/', function () {
return view('welcome')->with('name', 'Andrew');
});
Getting a route URL
Route::get('/', function () {
return view('welcome');
})->name('home');
route('home')
If a route has parameters:
route('home', ['id' => 1, 'name' => 'Andrew'])
Group with a name:
Route::prefix('admin')->name('admin.')->group(function () {
Route::get('welcome', function () {
return "Welcome";
})->name('welcome');
});
## route URL:
route('admin.welcome')
Route for absent URLs:
Route::fallback(function() {
return redirect()->route('home');
});
Abort function:
Route::fallback(function() {
abort(404, 'Oops! Page not found.');
});
In this case views should be in resources/views/errors/404.blade.php
<h1>{{ $exceptions->getMessage() }}</h1>
Calling controllers
Route::get('/', 'HomeController@index');
Route::get('/', 'Admin\PostController@index');
Resource controllers:
Route::resource('/photos', 'PhotoController');