Laravel 5 Resourceful Routes Plus Middleware - laravel

Is it possible to add middleware to all or some items of a resourceful route?
For example...
<?php
Route::resource('quotes', 'QuotesController');
Furthermore, if possible, I wanted to make all routes aside from index and show use the auth middleware. Or would this be something that needs to be done within the controller?

In QuotesController constructor you can then use:
$this->middleware('auth', ['except' => ['index','show']]);
Reference: Controller middleware in Laravel 5

You could use Route Group coupled with Middleware concept:
http://laravel.com/docs/master/routing
Route::group(['middleware' => 'auth'], function()
{
Route::resource('todo', 'TodoController', ['only' => ['index']]);
});

In Laravel with PHP 7, it didn't work for me with multi-method exclude until wrote
Route::group(['middleware' => 'auth:api'], function() {
Route::resource('categories', 'CategoryController', ['except' => 'show,index']);
});
maybe that helps someone.

UPDATE FOR LARAVEL 8.x
web.php:
Route::resource('quotes', 'QuotesController');
in your controller:
public function __construct()
{
$this->middleware('auth')->except(['index','show']);
// OR
$this->middleware('auth')->only(['store','update','edit','create']);
}
Reference: Controller Middleware

Been looking for a better solution for Laravel 5.8+.
Here's what i did:
Apply middleware to resource, except those who you do not want the middleware to be applied. (Here index and show)
Route::resource('resource', 'Controller', [
'except' => [
'index',
'show'
]
])
->middleware(['auth']);
Then, create the resource routes that were except in the first one. So index and show.
Route::resource('resource', 'Controller', [
'only' => [
'index',
'show'
]
]);

Related

How to take middleware on one function on route ressource (laravel)

I want take auth middleware on only create, update, delete route ressource. I know how to do with simple route, so, i don't know with ressource route.
Route::resource('cars', CarController::class);
Route::get('/cars/{car}/delete', [CarController::class, 'softdelete'])->name('cars.softdelete')->middleware('auth');
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth'])->name('dashboard');
You can put the middleware on your controller:
public function __construct()
{
$this->middleware('auth')->except(['index','show']);
// OR
$this->middleware('auth')->only(['store','update','edit','create']);
}
You can do that if you define the resource route with and without middleware separate: (quick example of the idea)
Route::resource('cars', CarController::class, [
'only' => [
'index',
'show'
]
]);
Route::resource('cars', CarController::class, [
'except' => [
'index',
'show'
]
])
->middleware([ 'auth']);

How can I make index form resource route out of the middleware?

Now this route's without ( except ) it just find working but when I want to reach any route like index or store I must to login then get the data form them . So I want to make index route out of this middleware ( it's ok reach to index without login ) hope to got it :)
route's :
Route::group(['middleware' => 'auth:api'], function() {
Route::resource('cards', 'cardsController', ['except' => 'index']);
Route::resource('services', 'servicesController', ['except' => 'index']);
Route::get('getUserinfo', 'LoginController#getUser');
});
and this my route list for cards :
and this when I get all cards form postman :
You have ['except' => 'index'] in the route group. Try to take it out and check if it is working.
Edited:
if you want index showing without login, try writting another route outside of the route group
so something like this
Route::get('cards', 'cardsController#index');
Route::get('services', 'servicesController#index');
Route::group(['middleware' => 'auth:api'], function() {
Route::resource('cards', 'cardsController', ['except' => 'index']);
Route::resource('services', 'servicesController', ['except' => 'index']);
Route::get('getUserinfo', 'LoginController#getUser');
});
First you should name your Controllers properly, instead of
cardsController
name it to
CardsController
Your error occurs because you are excepting the index action in your Controller.
Instead of this:
Route::resource('cards', 'cardsController', ['except' => 'index']);
Route::resource('services', 'servicesController', ['except' => 'index']);
Do this:
Route::resource('cards', 'cardsController');
Route::resource('services', 'servicesController');
Check out the docs
Dont forget to name your Controller properly.
As you should define your route outside the middleware.... As u want to use it without login....
Route::get('cards', 'cardsController#index');
Route::get('services', 'servicesController#index');
In middleware u have to define the route as below
Route::resource('cards', 'cardsController')->except('index');
Route::resource('services', 'servicesController')->except('index');

LARAVEL: How to use middleware on named routes

I've been working on an app that initially didn't use middleware. Later on, I decided to add middleware and had to change my routes from something like:
Route::get('admin/poems', array('as' => 'poems', 'uses' => 'PoemsController#poem'));
to
Route::get('admin/poem', ['middleware' => 'auth', 'uses' => 'PoemsController#poem']);
Now the disadvantage is that I had been redirecting to this route (poems) several times and adding middleware as indicated will require me to go through all my code and change the name of the route in the redirect.
How do i solve this problem?
Thanks for any help.
You don't need to lose the name of your route, the array will still accept it along with your middleware.
Just add it in to look like so:
Route::get('admin/poem', ['middleware' => 'auth', 'as' => 'poems', 'uses' => 'PoemsController#poem']);
This way you don't need to go through and rename your routes anywhere and can still protect it with auth middleware.
try put middleware to a group route
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () {
// Uses Auth Middleware
});
Route::get('user/profile', function () {
// Uses Auth Middleware
});
});

Add auth to some methods of API resource routes

I need to restrict access to the index resource so that people cant view all the submissions from a contact form.. is this possible to do via a route or what are people doing ?
Im using Laravel 5.2
Route::group(['prefix' => 'v1/api', 'middleware' => ['cors']], function(){
Route::resource('contact', 'ContactFormController', ['except' => [
'create', 'edit'
]]);
});
got it.. added this to the controller in question
function __construct() {
$this->middleware('auth', array('only' => array('index', 'show')));
}

Middleware for child route laravel 5.1

I'm looking to filter child routes for admins route, for example:
get('admins/*', ['middleware' => 'auth', function() {}]);
I think in Laravel 4 was Route::when('admins/*', '/'); to redirect user for / if not has auth by Call Pattern Filter from filter.php.
Is there someway to achieve that in Laravel 5.1?
You could set the admins path as a group and set the middleware on the whole group:
Route::group(['prefix' => 'admins', 'middleware' => 'auth'], function () {
Route::get('some_admin_page', function () {
# code...
});
});
Another way to achieve it in case all 'admins' routes are under the same controller you can set in the constructor to call the middleware
public function __construct() {
$this->middleware('auth');
}

Resources