Laravel 5.4
Awesome Laravel
- Awesome Laravel (Chirag Gude)
Prologue
- Release Notes
- Upgrade Guide
Getting Started
- Installation
- Configuration
- Directory Structure
- Laravel Homestead
- valet
Architecture Concepts
- Request Lifecycle
- Service Container
- Service Providers
- Facades
The Basics
- Routing
- Errors & Logging
- Middleware
- CSRF Protection
- Controllers
- HTTP Requests
- HTTP Responses
- Views
- HTTP Session
- Validation
Frontend
- Blade Templates
- Localization
- JavaScript & CSS Scaffolding
- Compiling Assets (Laravel Mix)
Security
- Authentication
- API Authentication (Passport)
- Authorization
- Encryption
- Hashing
- Resetting Passwords
Digging Deeper
- Artisan Console
- Queues
- Package Development
- Task Scheduling
- Broadcasting
- Cache
- Collections
- Events
- File Storage
- helpers
- Notifications
Database
- Database Getting Started
- Database Query Builder
- Database Pagination
- Database Migrations
- Database Seeding
- Redis
Eloquent ORM
- Eloquent Getting Started
- Eloquent Relationships
- Eloquent Collections
- Eloquent Mutators
- Eloquent Serialization
Testing
- Testing Getting Started
- HTTP Tests
- Browser Tests (Laravel Dusk)
- Database Testing
- Mocking
- redirect
Official Packages
- Laravel Cashier
- Envoy Task Runner
- Laravel Scout
redirect
Creating Redirects
Redirect responses are instances of the Illuminate\Http\RedirectResponse
class, and contain the proper headers needed to redirect the user to another URL. There are several ways to generate a RedirectResponse
instance. The simplest method is to use the global redirect
helper:
|
Sometimes you may wish to redirect the user to their previous location, such as when a submitted form is invalid. You may do so by using the global back
helper function. Since this feature utilizes the session, make sure the route calling the back
function is using the web
middleware group or has all of the session middleware applied:
|
Redirecting To Named Routes
When you call the redirect
helper with no parameters, an instance of Illuminate\Routing\Redirector
is returned, allowing you to call any method on the Redirector
instance. For example, to generate a RedirectResponse
to a named route, you may use the route
method:
|
If your route has parameters, you may pass them as the second argument to the route
method:
|
Populating Parameters Via Eloquent Models
If you are redirecting to a route with an “ID” parameter that is being populated from an Eloquent model, you may simply pass the model itself. The ID will be extracted automatically:
|
If you would like to customize the value that is placed in the route parameter, you should override the getRouteKey
method on your Eloquent model:
|
Redirecting To Controller Actions
You may also generate redirects to controller actions. To do so, pass the controller and action name to the action
method. Remember, you do not need to specify the full namespace to the controller since Laravel’s RouteServiceProvider
will automatically set the base controller namespace:
|
If your controller route requires parameters, you may pass them as the second argument to the action
method:
|
Redirecting With Flashed Session Data
Redirecting to a new URL and flashing data to the session are usually done at the same time. Typically, this is done after successfully performing an action when you flash a success message to the session. For convenience, you may create a RedirectResponse
instance and flash data to the session in a single, fluent method chain:
|
After the user is redirected, you may display the flashed message from the session. For example, using Blade syntax:
|