Controllers
Responsibilities
- Handle only HTTP requests
- Delegate business logic to actions, batches, or pipelines
- Return HTTP responses
- Use single action controllers
Naming
- Use singular nouns
- Use descriptive names that clearly communicate the controller's purpose
- Use the
Controller suffix
Location
- Place controllers in the
app/Http/Controllers directory
- If a project has HTTP and API routes split into
HTTP and API directories
- Use subdirectories to organize controllers
Order of Parameters
- Route parameters
- Request parameters
- Dependencies
Request Validation
- Use Form Requests to validate incoming requests
- Use the
rules method to define the validation rules
- Use the array syntax to define the rules (not the
| syntax)
- Create a Rule object for complex validation rules
Response
- Use the
response helper to return responses
- Use the explicit
response method
return response()->json($data, 200);
- Use the
redirect method to return redirects
- Use the
route method to redirect to a named route
return redirect()->route('home');