I have the following route definition in Laravel 5. When I group the routes in the following way, it seems the route admin.proposals.home will not work if I provide "/" as the get path, and will work if something trailing (like home) is provided:
/**
* Routes for system administrators.
*/
Route::group(['prefix' => 'admin', 'middleware' => 'admin'], function()
{
Route::group(['prefix' => 'people'], function()
{
Route::get('/', ['as' => 'admin.people.home', 'uses' => 'PersonController#index']);
Route::get('/profile/{userName}', ['as' => 'admin.person.profile', 'uses' => 'PersonController#view']);
Route::get('/organization/{id}', ['as' => 'admin.people.organization', 'uses' => 'PersonController#indexByOrganization']);
});
Route::group(['prefix' => 'projects'], function()
{
Route::get('/', ['as' => 'admin.projects.home', 'uses' => 'ProjectController#index']);
Route::get('/{projectId}', ['as' => 'admin.project.view', 'uses' => 'ProjectController#view']);
Route::group(['prefix' => 'proposals'], function()
{
//problematic line below
Route::get('/home', ['as' => 'admin.proposals.home', 'uses' => 'ProposalController#index']);
Route::get('/{proposalId}', ['as' => 'admin.proposal.view', 'uses' => 'ProposalController#view']);
});
});
});
Specifically, if I change the line:
Route::get('/home', ['as' => 'admin.proposals.home',
'uses' => 'ProposalController#index'
]);
To:
Route::get('/', ['as' => 'admin.proposals.home',
'uses' => 'ProposalController#index'
]);
I got an error saying:
Trying to get property of non-object (View: ... \views\admin\projects\view.blade.php)
But, the admin.proposals.home route points to the controllers index() method and has nothing to do with the view.blad.php.
Changing the path back to get('/home') works perfectly.
What is it that I'm missing?
It's a Route position order problem.
Since you have the route for the URI admin/projects/ as admin.project.home, and after that admin/projects/{projectId} it takes precedence over the route admin/projects/proposals/
Laravel takes proposals as the projectId.
Route::group(['prefix' => 'projects'], function()
{
Route::group(['prefix' => 'proposals'], function()
{
//problematic line below
Route::get('/', ['as' => 'admin.proposals.home', 'uses' => 'ProposalController#index']);
Route::get('/{proposalId}', ['as' => 'admin.proposal.view', 'uses' => 'ProposalController#view']);
});
Route::get('/', ['as' => 'admin.projects.home', 'uses' => 'ProjectController#index']);
Route::get('/{projectId}', ['as' => 'admin.project.view', 'uses' => 'ProjectController#view']);
});
Try this order and let me know what you get. But purely it's a route order problem.
Related
InvalidArgumentException Route [welcome.begin] not defined.
But it does? Happens when visiting /welcome/begin and I have double checked the file does exist there.
Routes:
<?php
Route::group(['domain' => 'localhost', 'namespace' => 'Frontend'], function () {
Route::group(['middleware' => 'guest', 'namespace' => 'Guest'], function() {
Route::group(['prefix' => 'welcome'], function() {
Route::any('/begin', ['as' => 'frontend.welcome_begin', 'uses' => 'WelcomeController#begin']);
Route::any('/language', ['as' => 'frontend.welcome_language', 'uses' => 'WelcomeController#language']);
Route::any('/final', ['as' => 'frontend.welcome_final', 'uses' => 'WelcomeController#final']);
});
Route::any('/', ['as' => 'login', 'uses' => 'LandingController#redirect']);
Route::get('/login', ['as' => 'frontend.login', 'uses' => 'LoginController#getView']);
Route::post('/login', ['as' => 'frontend.login', 'uses' => 'LoginController#onPost']);
});
Route::group(['middleware' => 'auth', 'namespace' => 'User'], function() {
Route::get('/home', ['as' => 'frontend.home', 'uses' => 'HomeController#getView']);
});
});
WelcomeController:
<?php
namespace App\Http\Controllers\Frontend\Guest;
use App\Http\Controllers\Controller;
use Redirect;
use Illuminate\Http\Request;
class WelcomeController extends Controller
{
public function begin()
{
return route('frontend.welcome.begin');
}
}
View Config:
<?php
return [
'paths' => [
resource_path('views'),
],
'compiled' => realpath(storage_path('framework/views')),
];
There error happens on the "route("frontend.welcome.begin")" line
The route does not exist. Your route is named as frontend.welcome_begin but you are calling frontend.welcome.begin
Your code will need to be: return route('frontend.welcome_begin');
I will suggest, having a brief look at the code, to change your welcome routes to be frontend.welcome.{name} rather than using an underscore, as it will follow the rest of the route names you've defined
First you have check if route name is correct
Open terminal
Go to project cd PATH_TO_PROJECT
write php artisan route:list
Check what is correct route name.
Everything is working fine except when I try to open some link that requires authentication I get the wrong route.
Route::group(array('prefix' => 'admin'), function(){
Route::get('login', array('as' => 'admin.login', 'uses' => 'AdminAuthController#getLogin'));
Route::post('login', array('as' => 'admin.login.post', 'uses' => 'AdminAuthController#postLogin'));
Route::get('logout', array('as' => 'admin.logout', 'uses' => 'AdminAuthController#getLogout'));
});
Route::group(array('prefix' => 'admin', 'before' => 'auth'), function(){
Route::get('dashboard', array('as' => 'admin.dashboard', 'uses' => 'AdminWorksController#dashboard'));
Route::get('/', array('as' => 'admin.dashboard', 'uses' => 'AdminWorksController#dashboard'));
Route::resource('works', 'AdminWorksController', array('except' => array('show')));
});
So when I do HTML::linkRoute('admin.login') the output link is fine, but when I try to open a route in the group with the filter 'before'=>'auth' without being logged in it redirects to /login instead of /admin/login.
Apart from that, everything works great. With the command php artisan routes looks good too.
What am I doing wrong? How can I fix this mistaken redirect of auth?
Change your auth filter in app/filters.php to this:
Route::filter('auth', function()
{
if (Auth::guest())
{
if (Request::ajax())
{
return Response::make('Unauthorized', 401);
}
return Redirect::guest(route('admin.login'));
}
});
I have been using laravel for a while now, but I stumbled across an error which I have never encountered before. It is probably me overlooking it, but with the route file given below, the route group with the prefix account gives a blank page. When going to /account/anunregisteredroute it does give a httpnotfoundexception
My routes.php file:
http://pastebin.com/EnnGSm10
By adding a / before the parameter you can solve this issue:
Route::get('/{username}', ['as' => 'account-profile', 'uses' => 'AccountController#getProfile']);
This piece of code worked for me:
Route::group(['prefix' => 'account'], function () {
Route::get('/{username}', ['as' => 'account-profile', 'uses' => function($username){
echo $username;
}]);
Route::get('profile', ['as' => 'account-edit-profile', 'uses' => 'AccountController#getUpdate', 'before' => 'auth']);
Route::post('profile', ['as' => 'account-edit-profile', 'uses' => 'AccountController#postUpdate', 'before' => 'auth|csrf']);
Route::group(['before' => 'guest'], function () {
Route::get('create', ['as' => 'account-create', 'uses' => 'AccountController#getCreate']);
Route::get('signin', ['as' => 'account-signin', 'uses' => 'AccountController#getSignin']);
Route::group(['before' => 'csrf'], function() {
Route::post('create', ['as' => 'account-create', 'uses' => 'AccountController#postCreate']);
Route::post('signin', ['as' => 'account-signin', 'uses' => 'AccountController#postSignin']);
});
});
});
I got the expected output.
I've noticed a bit of a peculiarity in Laravel 4 when using Routes. I have a Route group that looks like this:
// Employers routes
Route::group(array('prefix' => 'employers'), function(
Route::get('/', array('as' => 'employers.index', 'uses' => 'EmployersController#index'));
Route::get('create', array('as' => 'employers.create', 'uses' => 'EmployersController#create'));
Route::post('/', array('as' => 'employers.store', 'uses' => 'EmployersController#store', 'before' => 'csrf'));
Route::get('search', array('as' => 'employers.search', 'uses' => 'EmployersController#search'));
Route::get('{id}', array('as' => 'employers.show', 'uses' => 'EmployersController#show'));
Route::get('{id}/edit', array('as' => 'employers.edit', 'uses' => 'EmployersController#edit'));
Route::patch('{id}/update', array('as' => 'employers.update', 'uses' => 'EmployersController#update', 'before' => 'csrf'));
Route::delete('{id}/destroy', array('as' => 'employers.destroy', 'uses' => 'EmployersController#destroy', 'before' => 'csrf'));
));
I've noticed, however, that when I try and add in a new route I have to add it before the first route to use the {id} wildcard as the first parameter in it's url, otherwise I get a notfoundhttpexception. Is this normal? So for example, this works (adding in the employers.search route:
// Employers routes
Route::group(array('prefix' => 'employers'), function(
Route::get('/', array('as' => 'employers.index', 'uses' => 'EmployersController#index'));
Route::get('create', array('as' => 'employers.create', 'uses' => 'EmployersController#create'));
Route::post('/', array('as' => 'employers.store', 'uses' => 'EmployersController#store', 'before' => 'csrf'));
Route::get('{id}', array('as' => 'employers.show', 'uses' => 'EmployersController#show'));
Route::get('search', array('as' => 'employers.search', 'uses' => 'EmployersController#search'));
}
Results in the route employers.search not being found?
This is expected behavior. Routes are evaluated in a top-down fashion.
{id} is a "catch all" route.
So the route system sees /search - and thinks search is an {id} - so it loads that route. But then it cannot find an id of search - and so it fails.
So keep your "catch all" route at the bottom of the list - and it will work correctly.
Is there a way to make something like this?
Route::group(array('as' => 'admin', 'prefix' => 'admin', 'before' => 'admin'), function()
{
Route::get('/', array('as' => 'home', 'uses' => 'AdminController#index'));
Route::get('users', array('as' => 'users', 'uses' => 'AdminController#users'));
});
The goal is to do not include "admin" in all names and make links for above example like this:
URL::route('admin.home');
URL::route('admin.users');
Above example doesn't work:
Illegal offset type in unset
laravel/bootstrap/compiled.php:5053
Named group with nonamed routes inside works.
Named routes in nonamed group work too.
But not together.
Route::group(['prefix' => 'admin', 'before' => 'adminAuth'], function(){
// If you do not want to repeat 'admin' in all route names,
// define the value here
$r = 'admin';
Route::get('users', ['as' => "{$r}.users", 'uses' => 'AdminController#users']);
Route::get('/', ['as' => "{$r}.root", 'uses' => 'AdminController#index']);
});
In yout views/redirect you can use URL::action('ControllerName#method) and Laravel will know where redirect/point to...