Laravel 5.1 UrlGenerator route not defined error

10,937

If you follow the stack trace, you'll see that the route function basically invokes UrlGenerator->route

https://github.com/laravel/framework/blob/5.1/src/Illuminate/Routing/UrlGenerator.php#L300

Which checks if the route with that name exists, and if not, throws an InvalidArgumentException.

Basically, what you're trying to do is not possible. You must define that route if you want to use the route function.

I think your best option is to set up a "feature not developed" view, and point all of these pending routes to that view. That way you can use the route function. Your link/button will be generated, but will take them to a "not yet developed" page. Another benefit to this is that all of your routes are laid out and you can easily see which ones need to be developed.

Route::get('/brokers/contacts/create', ['uses' => 'HomeController@notDeveloped', 'as' => 'broker_contact_create']);

And inside your HomeController:

public function notDeveloped() {
    return view('pages.not_developed');
}
Share:
10,937
Shannon Phillips
Author by

Shannon Phillips

Updated on June 04, 2022

Comments

  • Shannon Phillips
    Shannon Phillips over 1 year

    I have an app built with Laravel 5.1. I'm using Form::open using a route_name to generate the url to post the form to as well as creating links by defining route and using the UrlGenerator. The issue is that I have some buttons/links created for display purposes and do not have pages created yet.

    I get a route not defined error with stack trace going back to UrlGenerator line 296. I'm wanting to set up something so that the error does not display. Instead, I would like a link to be generated to the pre-defined page that I have created saying that the feature the user clicked on is not yet developed.

    I thought about doing something similar to a 404 error, but the issue is that the existing page (the page the link or button lives on) is not being displayed, not just that route is missing.

    For example, below, I create a link to the route "broker_contact_create" Since this route does not exist, the page displaying the link will not load. Instead, I get the error saying:

    ErrorException in UrlGenerator.php line 296: Route [broker_contacts_create] not defined. (View: index.blade.php)

    <div class="col-md-6 col-lg-7 margin-bottom-15">
        <a href="{{ route('broker_contacts_create') }}" class="btn btn-success btn-icon-plus">+ Add Contact</a>
    
    </div>
    

    Instead, I want the page to be displayed. When the user clicks on the link to a missing route, to have them routed to a page that tells the user they clicked on a link to a feature that has not been enabled yet.

    So basically I just want it to do: if route not found then provide $url.