How to conditionally add middleware in another middleware Laravel - laravel

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).

Related

Laravel Custom Request Class - can I define Middleware here?

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

Routing via Laravel API

I have a very small software and I use laravel and vue.js. I would like to know the difference between routing though api.php and web.php in routes folder. Can somebody explain me the difference in these two cases?
You can define the routes in either web.php or api.php. However, routes in api.php have certain advantages
routes get automatically prefixed with '/api/'
routes use api-middleware and auth. This can add certain additional security by throttling API-requests.
You should use api.php for all your api needs or any routes that will be called through ajax.
:

Laravel routing pattern similar to Yii

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

Laravel limiting access to route

I am trying to implement a basic image fetch system for my website. Already created a route that returns me the image.
what concerns me is that i want that route to be only accessible by certain controllers.
Tried to search it and found out passport might be viable option but it's pretty complex for this app. Are there any possible options ?
EDIT:
Sorry for providing insufficient information. I want the route to be accessible only by CONTROLLERS, not by anyone who enters the route url to address bar. Like using it as an api maybe.
There several ways to achieve that, you can use middleware, you can consider using packages like entrust which also require you to have some knowledge about using middleware. or use laravel Auth
create a table add all the routes in that table and then check the allowed route in AppService provider.
$routename = Request::route()->getName();
$allowed_route = AllowedRoutes::where("route","=",$routename)->count();
if($allowed_route == 0)
exit();

Laravel 5.1 flash message after register

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

Resources