Laravel 5.4 Modules: NotFoundHttpException in RouteCollection.php line 161 - php

Yep, it's another Laravel route not found issue! This is actually quite an odd one. I am aware there are multiple issues here with the same error message at least, but none that seem to be the same as mine.
I am using the nWidart/laravel-modules (https://github.com/nWidart/laravel-modules) package for Laravel to create a module-based setup. I began with a fresh install of Laravel 5.4 via Composer, then generated the basic user authentication via php artisan make:auth. After basic setup, my app/Http/routes.php file looks like this:
// home page
Route::get('/', function () {
return view('welcome');
});
// login, logout, register, etc.
Route::auth();
// admin dashboard
Route::get('/admin', 'AdminController#index');
I then installed the modules package via Composer and ran php artisan module:make Finances Jobs Etsy to create three modules, Finances, Jobs, and Etsy. This ran without incident. I then went into the Http/routes.php file for each individual module and added the auth middleware and an additional prefix to each route group:
// Finances routes file
Route::group(['middleware' => ['web', 'auth'], 'prefix' => 'admin/finances', 'namespace' => 'Modules\Finances\Http\Controllers'], function()
{
Route::get('/', 'FinancesController#index');
});
// Jobs routes file
Route::group(['middleware' => ['web', 'auth'], 'prefix' => 'admin/jobs', 'namespace' => 'Modules\Jobs\Http\Controllers'], function()
{
Route::get('/', 'JobsController#index');
});
// Etsy routes file
Route::group(['middleware' => ['web', 'auth'], 'prefix' => 'admin/etsy', 'namespace' => 'Modules\Etsy\Http\Controllers'], function()
{
Route::get('/', 'EtsyController#index');
});
Now here is the very frustrating issue: all the base app routes work fine. The home page route, the auth routes, and the /admin route all work perfectly. In addition, all the Jobs module routes work perfectly as well. However, any and all routes from the Finances and Etsy modules do not work. Trying to access https://[site url]/admin/finances shows me the following error:
NotFoundHttpException in RouteCollection.php line 161:
Which to me says, Laravel can't find the given route, despite being able to find the route for the Jobs module.
Extra confusingly, it sort of seems like Laravel can find the routes, as running php artisan route:list DOES show me the routes for the Finances and Etsy modules.
I have also tried running php artisan cache:clear and php artisan route:clear multiple times, in case it was some sort of caching issue. No luck.
So at this point I have absolutely no idea what to make of all this. I've got three modules, one whose routes are working perfectly, and two others, whose identically-configured routes are not working at all, yet are being found by route:list. I'm pretty much out of ideas as to what the issue could be at this point.
UPDATE: interestingly, if I place the route in the base app routes.php file like so:
Route::group(['middleware' => ['web', 'auth'], 'prefix' => 'admin/finances'], function()
{
Route::get('/', '\Modules\Finances\Http\Controllers\FinancesController#index');
});
Then the route does work. So it seems to be an issue where the web application specifically can't seem to find the routes.php files for those two modules, but the console application can.

I has the same issue, I solved it adding name property in:
Module/[ModuleName]/Http/routes.php,
For example:
Route::get('alias','controller#functionName')->name('module.alias');
I hope this tip could help you!

I had the same error and it solved like this.
Change file modules.php :
'scan' => [
'enabled' => false,
'paths' => [
base_path('vendor/*/*'),
],
],
To this:
'scan' => [
'enabled' => true,
'paths' => [
base_path('modules/*'),
],
],
And then run:
php artisan route:clear
php artisan optimize

Related

Can't access to certain page from my website using Laravel and Authentification

I hope you are well. I have a site under Laravel using the OVH could web. I connect in FTP in order to update a file (the web.php file, it allows to define the routes of the site). I had put a middleware on some routes but I removed them in ftp. I return to my site but it still asks me to log in. So I tried it locally and this change works fine.
I'm using the Breeze framework.
Here is an example of what I had in route.php :
Route::group(['prefix' => 'test', 'middleware' => 'auth'], function(){
So I removed the 2nd part, which gives:
Route::group(['prefix' => 'test'], function(){
But it still doesn't work. Do you know if there is another place where I could indicate that an authentication is required in order to go to the page please?
Cordially

Log in asking for login in on every screen in Laravel 5.3

I'm updating my app to Laravel 5.3 and having a lot of trouble with middleware.
Every time I go to a route that is protected, it asks me to log in.
All of my protected routes are in a route group:
Route::group(['middleware' => ['web', 'auth']], function()
{
// all routes here
});
I've also tried:
Route::group(['middleware' => ['auth']], function()
And:
Route::group(['middleware' => 'auth'], function()
I've also tried adding ->middleware('auth') to the end of the individual routes. I've even done a clean Laravel install and added routes one at a time until I found the problem. That problem is, unfortunately, navigating between auth routes.
Nothing is helping.
Does anyone have any advice? I'm at a standstill updating my app until I get this solved.

Custom defined routes not resolving in Laravel

I have a Laravel 5.2 instance utilizing all the typical out-of-the-box routes for dashboard, cases, home, login/logout, and users (which is working well). I now need to create a wizard with steps (step1, step2, step3, etc.), and I need access to the session. I assigned them to the group middleware.
Route::group(['middleware' => 'web'], function () {
Route::get('/', function () {
// Uses Web middleware
});
Route::get('wizard/step1', [
'as' => 'wizard/step1', 'uses' => 'Wizard\WizardController#getStep1']);
Route::get('wizard/step2', [
'as' => 'wizard/step2', 'uses' => 'Wizard\WizardController#getStep2']);
});
However, when I go to the named route(s), I get a 404 error. So WizardController looks like the following.
namespace App\Http\Controllers\Wizard;
use App\Http\Controllers\Controller;
use App\Http\Requests;
class WizardController extends Controller
{
public function __construct()
{
//$this->middleware('guest');
}
public function getStep1()
{
return view('wizard.step1');
}
}
The defined views are resources/views/wizard/step1.php. Ideally, I'd like to refactor it so that Wizard is a separate group. However, nothing seems to work with the way I'm defining custom routing currently.
This happens when you cache the routes. The new route entries you add will never be recognized unless you clear the route cache.
You can delete cached routes using php artisan route:clear.
Since you will be changing the routes frequently in dev env, It is always better to not perform route caching while in dev environment.
You can do it by only running artisan route:cache as a post-deploy hook in Git, or just run it as a part of your Forge deploy process. So that every time you deploy your code in your server, your routes are cached automatically.
I resolved my issue by running the command:
php artisan route:clear
This lets Artisan delete your route cache and you can you can make sure routes are in sync.
Thanks for the help!

Laravel 5 Cached Routes not updating

I've cached my Laravel 5 routes by doing php artisan route:cache.
This went succesfull for quite a while and when I changed a route is could cache them again and it would all work like it should.
The problem is that I've moved some routes to another route group and it seems that they won't be cached for some reason.
I've tried to cache them again but it didn't work. I've also tried php artisan cache:clear but still not working.
Routes.php changes:
Route::group(['prefix' => 'api', 'middleware' => 'auth'], function () {
Route::get('invites', 'InvitationController#get');
Route::get('invites/check', 'InvitationController#check');
});
Changed to:
Route::group(['prefix' => 'api'], function () {
Route::post('auth', 'Auth\AuthController#authenticate');
Route::get('invites', 'InvitationController#get');
Route::get('invites/check', 'InvitationController#check');
});
As you can see I've moved those invitation routes to the Route group without the Authenticate Middleware. When I cached the routes again, it still does execute the Auth Middleware even when they are moved out of the group..
The right syntaxe to remove cached route in laravel 5.* is
php artisan route:clear
Regards
Try:
php artisan optimize
This will clear all the route cache as well as the file cache.
If you don't want to optimize again with every change.
You can try this;
CACHE_DRIVER=file to change => CACHE_DRIVER=array
After, you should clear to folder "bootstrap/cache"
I hope it works.

Laravel routing on XAMPP with virtual hosts

I'm learning laravel, and have been following this phpacademy tutorial: http://www.youtube.com/watch?v=gf6yNkYVCjs I have virtual hosts setup with XAMPP Apache and that's all good.
In the routes.php file we have this:
Route::get('/', array('as' => 'home', 'uses' => 'HomeController#getIndex'));
Route::get('/login', array('as' => 'login', 'uses' => 'AuthController#getLogin'));
Route::post('/login', array('uses' => 'AuthController#postLogin'));
I type into my browser "tutorial" and I get the homepage as per the tutorial.
The problem is when I append login as in "tutorial/login" then I get a 404 error, object not found.
The AuthController etc is all in place, but I suspect that I have a routing problem, I just can't figure out where.
Update:
If I just use the php inbuilt server "php artisan serve" then it all works fine. So It looks like more of an Apache config problem than a laravel problem.

Categories