So, for me the main 2 methods of adding middleware are in the routes files, api.php and web.php, or alternatively in the constructor of any Controller class.
However it occurred to me that there might be situations where I want to choose for a certain Request subclass to have some middleware applied to it. I haven't seen documentation for this. Is there a way to define middleware in these custom Requests?
For clarity, I'm referring to the Request classes that get made when you do php artisan make:request
Related
I'm wondering how I can add middleware conditionally in another middleware in my Laravel project.
I noticed that Laravel Nova adds middleware in this way in the NovaCoreServiceProvider
$this->app->make(HttpKernel::class)->pushMiddleware(ServeNova::class);
I am trying to accomplish this outside of the scope of a service provider, where you do not have access to $this->app, so I tried it this way:
$container = app();
$container->make(HttpKernel::class)->pushMiddleware(MyMiddleware::class);
But this doesn't seem to work. Is it possible to push middleware to the stack from a middleware? If so, how can I accomplish this?
The only reason I am doing it this way as opposed to just simply registering it normally is to prevent the unnecessary loading of resources (which the middleware is responsible for triggering).
I am new to Laravel and by reading a bit its documentation, it seems that for each new page or url I need to define a route in the web.php file. Please correct me if I am wrong.
So, my question is... Is there a way to create a pattern as in YII framework or .NET framework to manage all routes (or most of them) at once by using something like:
{controller}/{action}/{id}
Well not exactly like Yii or .net but you can declare multiple routes in one line using Resource Controllers.
Laravel resource routing assigns the typical "CRUD" routes to a controller with a single line of code. For example, you may wish to create a controller that handles all HTTP requests for "photos" stored by your application.
You can do so by Route::resource('photos', 'PhotoController');
Read more here
I've inherited a codebase that I'm trying to prune - looking at middlewares I see some that I can't see being used in routes, nor controllers, but I'm worried that I'm not taking into account all scenarios.
For example - there's this middleware: '2fa' => \PragmaRX\Google2FALaravel\Middleware::class that I can't see being used anywhere, yet when I comment it, our 2fa functionality fails to work.
As such, the question is - is there a way to see where given middlewares are used?
PS. I'm talking here purely about middlewares references within $routeMiddleware array
This will give you a list of all the routes in your application, with the middleware applied
Go to your project root and write in
php artisan route:list
if it's too many records, use grep
php artisan route:list | grep "fa"
if It's not being applied to the routes file, try viewing in the controllers or the RouteServiceProvider
Edit:
If your project is using its facade, you can try and search for use Google2FA in your project
I'm using Laravel's out of the box authentication, which came with laravel 5.
Basically, my AuthController is empty, and all the logic happens within the AuthenticatesAndRegistersUsers trait.
Now I want to flash a message only after a user is registered.
I can change the trait, but it's not recommended, and might not work on future versions of Laravel.
There is a way to run my custom code from the Controller after user registers?
Just copy the trait into your controller, Laravel will use the method in your controller instead of the trait because it's kind of inheritance, that way you won't touch the trait and you will be sure that the method of the flash message will work on any version of Laravel
Is it possible to have custom urls without renaming controller methods when using RESTful routing with Route::controller?
With Route::controller('users', 'UsersController'); I want the url to be users/registration instead of users/create without renaming getCreate method in UsersController.
Yes you can. Before Route::controller('users', 'UsersController'); in your routes, you can add the following line:
Route::get('users/registration', 'UsersController#getCreate');
By doing that you're explicitly specifying that you want the GET request for users/registration to be handled by getCreate method on the UsersController class.