Laravel 5 routing-resource issue - laravel

I am trying to use Laravel 5's resource inside group and it is not working
Route::group([
'prefix' => 'apps',
'namespace' => 'App',
], function () {
Route::resource('/', 'AppController', ['only' => ['show', 'index']]);
//but if I am adding something after / it works
Route::resource('/asd', 'AppController', ['only' => ['show', 'index']]);
Route::get('/{slug}/{userId}', 'AppController#shared');
});
I am doing something wrong, or it is simply not allowed?
Thanks.

I think it's not gonna work. You could try to use:
Route::resource('something', 'AppController', ['only' => ['show', 'index']]);
And then use URLs like /apps/something/slughere to call #show action.
You can look at your current working routes by calling this command:
php artisan route:list
If you'll run it now, you will see something like this:
apps/{} | apps..show
Which is not working route.

Try putting
Route::resource('/', 'AppController', ['only' => ['show', 'index']]);
at the bottom

Related

How to make differently route name in Laravel?

I have two the same controller in different directories.
And routing is for both:
Route::resource('dashboard/statistic', 'Admin\StatisticController');
Route::resource('statistic', 'StatisticController');
When I run php artisan route:list
I see, that these routes have the same route name as: statistic:
statistic.index
statistic.destroy
statistic.edit
How can I make this diffrently?
You could to create each route explicitly (Route::resource creates multiple routes to handle a variety of RESTful actions on the resource), for example
Route::get('dashboard/statistic', ['as' => 'dashboard-statistic.index', 'uses' => 'Admin\StatisticController#index']);
Route::delete('dashboard/statistic', ['as' => 'dashboard-statistic.destroy', 'uses' => 'Admin\StatisticController#destroy']);
Route::put('dashboard/statistic', ['as' => 'dashboard-statistic.edit', 'uses' => 'Admin\StatisticController#edit']);
Maybe
Route::resource('dashboard/statistic', 'Admin\StatisticController',
['admin' => ['create', 'store', 'update', 'destroy']]);
Route::resource('statistic', 'StatisticController');
Hopefully this will solve your problem
maybe this can solve the problem
Route::group(['prefix' => 'dashboard', 'as' => 'dashboard.'], function() {
Route::resource('statistic', 'Admin\StatisticController');
});
will return name eg:
dashboard.statistic.store

Laravel 5 Resourceful Routes Plus Middleware

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'
]
]);

Routing confusion in laravel 4

I'm experiencing routing confusion in laravel 4.
Route::group(['prefix' => 'myProfile', 'before' => 'auth|inGroup:Model|isMe'], function()
{
Route::get('/{username}', function(){
echo 'hello';
});
});
Route::get('/{username}', [
'as' => 'show-profile',
'uses' => 'ProfileController#index'
]);
When i write to address bar domain.app/myProfile it runs second route and runs ProfileController#index...
Thanks.
Looks like correct behaviour. To access first route you would have to type something like domain.app/myProfile/FooUser. You didn't specify / route in myProfile route group, so it cannot match it and uses second one.
Breaking down your routes:
1)
Route::get('/{username}', [
'as' => 'show-profile',
'uses' => 'ProfileController#index'
]);
Use /example URI to access the above route.
2)
Route::group(['prefix' => 'myProfile', 'before' =>'auth|inGroup:Model|isMe'], function()
{
Route::get('/{username}', function(){
echo 'hello';
});
});
Use /myProfile/example URI to access the above route.
Your application is working as expected.

Facing an issue with routing - Laravel

I started working with Laravel last week and I'm facing a minor problem with routes.
when i do the following:
Route::group(array('before' => 'auth'), function() {
Route::resource('admin', 'VacatureController');
Route::get('admin/test', array('uses' => 'VacatureController#create'));
Route::post('admin/test', array('uses' => 'VacatureController#store'));
});
and i go to admin/test, i get an empty page.
when i change admin/test to something like test/test like:
Route::group(array('before' => 'auth'), function() {
Route::resource('admin', 'VacatureController');
Route::get('test/test', array('uses' => 'VacatureController#create'));
Route::post('test/test', array('uses' => 'VacatureController#store'));
});
it works fine. I looked itup in the documentation, but i didn't become anything wiser.
Can someone please enlighten me?
Try putting the Route::resource as the last route. Laravel will try all routes in the order you put them in the route file, so when you put the resource route first only this one will be checked because it expects all admin routes to be there.
Route::group(array('before' => 'auth'), function() {
Route::get('admin/test', array('uses' => 'VacatureController#create'));
Route::post('admin/test', array('uses' => 'VacatureController#store'));
Route::resource('admin', 'VacatureController');
});

Laravel Routes Artisan

When I run artisan routes in laravel 4
auth/login/{v1}/{v2}/{v3}/{v4}/{v5}
Is this normal or is there something wrong. My routes work just wondering if there might be a bug or something. Below are my routes for auth. I'm using restful routes for auth.
Route::controller('auth','AuthController');
Route::get('AuthController/login', array('as' => 'login', 'uses' => 'AuthController#login'));
Route::get('auth/logout', array('as' => 'logout', 'uses' => 'auth#logout'));
Route::post('auth/login', array('uses' => 'auth#login'));
This is expected. When you register controllers with Route::controller() the controller inspector adds the URI wildcards. Consider the following example:
Route::controller('user', 'UserController');
You might then have a method like this on your UserController:
public function getProfile($username)
{
$user = User::where('username', $username)->first();
return View::make('profile')->with('user', $user);
}
You could then hit that method by going to localhost/yourapp/user/profile/jason
In a nut shell it allows you to pass extra parameters to a method. To me this is a very old school way of doing it, as it looks nicer like localhost/yourapp/user/jason/profile and in this case you'd need to use a route to map to the controller method.
I suggest you 2 improvements:
1 - keep a standard with URIs
You don't need Route::controller in this case. In order to mantain all routes with the same structure I would do:
Route::group( array('prefix'=>'auth,function(){ //make all auth routes starting by auth
Route::get('getLogin', array('as' => 'getLogin', 'uses' => 'AuthController#getLogin'));
Route::get('getLogin', array('as' => 'logout', 'uses' => 'AuthController#logout'));
Route::post('postLogin', array('as' => 'postLogin', 'uses' => 'AuthController#postLogin'));
});
It is not necessary to use group but if you app grows could be better. Without group code will be:
Route::get('auth/getLogin', array('as' => 'getLogin', 'uses' => 'AuthController#getLogin'));
Route::get('auth/getLogin', array('as' => 'logout', 'uses' => 'AuthController#logout'));
Route::post('auth/postLogin', array('as' => 'postLogin', 'uses' => 'AuthController#postLogin'));
2 - Protect your post routes
For every post and put request we've to prevent CSRF attacks like this:
Route::post('postLogin',array('before' => 'csrf','uses'=>AuthController#postLogin) );

Resources