Skip to content
Advertisement

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' => 'WizardWizardController@getStep1']);
    Route::get('wizard/step2', [
        'as' => 'wizard/step2', 'uses' => 'WizardWizardController@getStep2']);
});

However, when I go to the named route(s), I get a 404 error. So WizardController looks like the following.

namespace AppHttpControllersWizard;

use AppHttpControllersController;
use AppHttpRequests;

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.

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement