Multiple middlewares for routes in Laravel. Array or multiple arguments? - laravel

I am building a website that, in order to check some of the content, you need to be authenticated AND verified.
When I enclose the middlewares in an array, it works.
Route::resource('premiumContent', 'PremiumContentController')
->middleware(['auth', 'verified']);
However, when I follow the original documentation,
https://laravel.com/docs/5.8/middleware#registering-middleware
grouping the middlewares passing them as multiple parameters, it does not work.
Route::resource('premiumContent', 'PremiumContentController')
->middleware('auth', 'verified');
Only the 'auth' middlewares applies, thus letting me access the content even if I am not verified. Why? What is the right approach?

Change the web.php code.This worked for me.
Route::group(['middleware' => ['auth', 'verified']], function() {
Route::resource('premiumContent','PremiumContentController');
});

Related

How to write laravel 5.8 controllers routes similar to laravel 5.1

Having more than 20 controllers. It's very difficult to set each and every routes for add, edit and delete (also having more actions).
This is my laravel 5.1 routes.php :
Route::controllers([
'user' => 'UserController',
'taxes' => 'TaxController',
]);
Is there any way to support these routes in laravel 5.8?
You can use the Resource Controller and implement in routes/web.php. It will autogenerate the name for the route
//web.php
Route::resource('user', 'UserController');
Route::resource('taxes', 'TaxController');
Edit 1
If you want to exclude show method of the controller for the resource, You can add array inside the except method.
Route::resource('taxes', 'TaxController', [
'except' => ['show']
]);
Further, if you want to get only selected options, You can use only.
Route::resource('taxes', 'TaxController', [
'only' => ['index', 'create', 'store', 'edit']
]);
The controllers method was deprecated in Laravel 5.2. From the upgrade guide:
Implicit controller routes using Route::controller have been deprecated. Please use explicit route registration in your routes file.
1) Use Resource Routes
Provided that your controllers use the standard index, store, show etc methods you can simply use resource routes. For example:
Route::resource('user', 'UserController');
However if you want to exclude certain methods you can add them to the resource. For example:
Route::resource('user', 'UserController', ['except' => 'show']);
2) Declare Routes Explicitly
You can follow the Laravel 5.2 upgrade guide as above and instead declare each route explicitly.
3) Create a Macro
The Laravel router is Macroable. This means that you can add your own methods to it. For example, in your app service provider you could have the following:
Illuminate\Routing\Router::macro('controllers', function ($routes) {
// Create your own implementation of the controllers method.
});
This allows you to create your own implementation of the controllers method which means you wouldn't need to alter your routes or controllers, but you may need to dive in and look at Laravel's route handling to understand how to implement this.
I hope this helps.
You can use in the array, In as you call using routes. like {{route('claimsubmit')}}
Route::resource('claimform',array('as'=>'claimform','uses'=>'UserController#claimform');

How does Laravel 5.4 Middleware work?

I've read manual of Laravel 5 but still don't find a solution for my problem.
My task: I need to run multiple websites on the one codebase.
What have I done for it? I made one main website and create table "sites" where I can add new satellites. It's all worked fine. But now, in front end part, I need to create middleware which will help me to divorce different parts of my engine.
I start to create middleware but I've only found 2 ways of middleware:
redirect to the correct URL
return $next($request) which means that this middleware will be always working.
I don't need a redirect. I just need one thing:
I need that in my web.php route file I can make routes with my middleware group:
For example:
Route::group(['middleware' => 'site'], function () {
//there will be only routes for my satellites
And
Route::group(['middleware' => 'web'], function () {
//There will be another routes which won't be on satellites
The problem is if I make a redirect on the main site my middleware still working and going into the cycle.
Is there any way to break middleware and don't going into the certain routes?
I've tried with HttpException but I've received only 404 or 500 errors, but I don't need it in this case.

Laravel using an OR style combination in Route Middleware

I am trying to use multiple middleware in a route as follows
Route::get('devices', ['as' => 'devices.index', 'uses' => 'deviceController#index', 'middleware' => ['permission:manage_devices', 'permission:device.view']]);
This will implement AND logic between the two middlewares. I want to implement an 'OR' Logic but could not find any help.
Tough it isn't the most eloquent solution, you could try to create your own middleware that loads other middleware in a if/else or switch statement. That way you might be able to achieve the behaviour you'd want.
Check the docs on the link below on how to program your own middleware:
https://laravel.com/docs/5.3/middleware#defining-middleware

Why my routes are not ordered?

in my routes file I use a route group with segment like:
Route::group(['prefix' => request()->segment(1) ], function(){
//routes
});
Normally, my routes are in the order they were written at, but when using the group with request()->segment(1) routes just get disarranged (not just inside the group itself, but all of them), I need to use segment so every customer will have their own slug as the first segment in the URL.
so how can I fix this issue?
Use middleware to validate the user and allow them based on their slug
Please Make own middleware like jwt.auth
I developed my own middleware to filter slug based

Laravel 5 - Assign Middleware to Controller Namespace

I need to do an ACL check for the user before allowing access to the admin panel of a Laravel 5 website. What is the best way to do this for an entire controller group in the namespace App\Http\Controllers\Admin\*? Ultimately, I'm looking for a "set and forget" method to do this, and middleware looks like the best option so far.
The initial idea was to assign a middleware to the admin route, but this does not prevent any other non-admin route from accessing the controllers. This means a route can still target the admin controllers and bypass the ACL check.
The next idea was to insert the assignment in the constructor on the controllers, but this would require each additional controller to explicitly include the middleware. This would require a developer to know that the middleware should be included, which allows them to miss it entirely. This also applies to using one base controller as the parent for all admin controllers, since the developer would need to know that the base controller should be extended. Right now, this looks like the best solution.
This leads us back to the question: can middleware be assigned to a controller wildcard namespace like App\Http\Controllers\Admin\*? Or, is there a better way for the ACL check to never need to be explicitly assigned to every admin controller?
This leads us back to the question: can middleware be assigned to a controller wildcard namespace like App\Http\Controllers\Admin*?
No
The most simplest approach you can do is create a base controller such as App\Http\Controllers\Admin\Controller and include the middleware
while all other App\Http\Controllers\Admin\* extends it.
Alternatively, while still adding App\Http\Controllers\Admin\Controller, you could instead inject the middleware through IoC Container.
App::afterResolving('App\Http\Controllers\Admin\Controller', function ($controller) {
$controller->middleware('acl');
});
EDIT
My previous answer didn't quite work for all situations; it broke a lot of other routes. Here's what I ended up doing instead (in app/Http/routes.php):
Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'middleware' => 'App\Http\Middleware\Acl'], function()
{
Route::get('/', 'AdminController#index');
Route::get('/user', 'User\UserController#index');
...
});
This at least targets all admin controllers when I define a route. It's not exactly everything I had hope for, but it will do.

Resources