Routing in Laravel refers to defining URL paths that users can access and mapping them to specific controller actions or closure callbacks. Routes are defined in files within the routes directory, primarily in web.php and api.php.
Controllers can be created using the Artisan command: php artisan make:controller ControllerName. This creates a new controller class in app/Http/Controllers directory with basic structure and necessary imports.
Controller middleware filter HTTP requests before they reach controllers. They can be assigned using the middleware method in controllers or in route definitions. They're defined in app/Http/Kernel.php and can be applied to specific methods using 'only' and 'except'.
Route constraints limit how route parameters may be matched using regex patterns. They can be applied using where() method or globally in RouteServiceProvider. Example: Route::get('user/{id}')->where('id', '[0-9]+').
Signed routes are URLs with a signature hash to verify they haven't been modified. Created using URL::signedRoute(), they're useful for email verification, temporary access links, or public sharing of protected resources.
API resource collections with conditional relationships use whenLoaded() method and conditional attribute inclusion. Custom collection classes can be created to handle complex transformations and relationship loading.
Laravel supports common HTTP methods: GET for retrieving data, POST for creating new resources, PUT/PATCH for updating existing resources, DELETE for removing resources. These can be defined using Route::get(), Route::post(), Route::put(), Route::patch(), and Route::delete().
A controller is a PHP class that handles business logic and acts as an intermediary between Models and Views. Controllers group related request handling logic into a single class, organizing code better than closure routes.
Routes can be named using the name() method: Route::get('/profile', [ProfileController::class, 'show'])->name('profile'). Named routes provide a convenient way to generate URLs or redirects using route('profile').
Redirects can be performed using redirect() helper or Route::redirect(). Examples: return redirect('/home') or Route::redirect('/here', '/there'). You can also redirect to named routes using redirect()->route('name').
CORS can be handled using the cors middleware and configuration in config/cors.php. Laravel provides built-in CORS support through the fruitcake/laravel-cors package. Headers and allowed origins can be configured as needed.
Dependency injection in controllers automatically resolves class dependencies in constructor or method parameters through Laravel's service container. This enables better testing and loose coupling of components.
Custom route model binding can be implemented by overriding the resolveRouteBinding() method in models or by defining custom binders in RouteServiceProvider's boot method using Route::bind().
Custom response macros extend Response functionality using Response::macro() in a service provider. Example: Response::macro('caps', function ($value) { return Response::make(strtoupper($value)); });
Route caching improves performance using 'php artisan route:cache'. It requires all route closures to be converted to controller methods. Route cache must be cleared when routes change using 'route:clear'.
Custom middleware parameters are implemented by adding additional parameters to handle() method. In routes: ->middleware('role:admin,editor'). In middleware: handle($request, $next, ...$roles).
Route fallbacks are implemented using Route::fallback(). Custom 404 handling can be done by overriding the render() method in App\Exceptions\Handler or creating custom exception handlers.
Conditional middleware can be implemented using middleware() with when() or unless() methods. Can also be done by logic in middleware handle() method or by creating custom middleware classes with conditions.
Multiple parameter binding can be implemented using explicit route model binding in RouteServiceProvider, or by implementing custom resolution logic in resolveRouteBinding(). Supports nested and dependent bindings.
web.php contains routes for web interface and includes session state and CSRF protection. api.php is for stateless API routes, includes rate limiting, and is prefixed with '/api'. api.php routes don't include session or CSRF middleware.
Route model binding automatically resolves Eloquent models from route parameters. It can be implicit (type-hint the model) or explicit (define in RouteServiceProvider). Example: Route::get('/users/{user}', function (User $user) { return $user; }).
File uploads are handled using the $request->file() method. Files can be stored using Storage facade or move() method. Example: if($request->hasFile('photo')) { $path = $request->file('photo')->store('uploads'); }
API versioning can be implemented using route prefixes, subdomains, or headers. Common approaches include using route groups with prefixes like 'api/v1/' or namespace-based versioning in different controllers.
Rate limiting is implemented using throttle middleware. It can be configured in RouteServiceProvider and customized using the RateLimiter facade. Limits can be set per minute/hour and customized per user or IP.
Domain routing is implemented using Route::domain(). Subdomains can capture parameters: Route::domain('{account}.example.com')->group(function () {}). Wildcard subdomains and pattern matching are supported.
Route parameters are dynamic segments in URLs that capture and pass values to controller methods. They are defined using curly braces in route definitions, like '/user/{id}' where {id} is the parameter.
Request data can be accessed using the Request facade or by type-hinting Request in controller methods. Example: public function store(Request $request) { $name = $request->input('name'); }
Route grouping allows you to share route attributes (middleware, prefixes, namespaces) across multiple routes. Routes can be grouped using Route::group() or using the Route::prefix() method.
Resource controllers handle CRUD operations for a resource. Created using 'php artisan make:controller ResourceController --resource', they provide methods like index(), create(), store(), show(), edit(), update(), and destroy(). They're bound to routes using Route::resource().
Soft deleted models in route binding can be included using withTrashed() scope. Custom resolution logic can be implemented in resolveRouteBinding() to handle different scenarios of soft deleted models.