Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel DomainException in RouteCompiler Variable name cannot be longer than 32 characters in route pattern

I'm getting this error because of a long variable name. How do I override the variable name?

I would very much like to keep the resource name.

My route is:

Route::resource(
    'computer-software-version-installation',
    'Web\Model\ComputerSoftwareVersionInstallationController'
);
like image 883
Josh Petitt Avatar asked Dec 27 '16 17:12

Josh Petitt


2 Answers

Here is what I did to fix:

    Route::resource(
        'computer-software-version-installation',
        'Web\Model\ComputerSoftwareVersionInstallationController',
        ['parameters' => [
            'computer-software-version-installation' => 'installation'
        ]]
    );

Why is this necessary?

For Laravel 5.3, here is the extended answer:

The Illuminate\Routing\Router resource method creates a new ResourceRegistrar to register the routes. This method as an $options parameter.

The Illuminate\Routing\ResourceRegistrar register method will set the parameters attribute to the $options['parameters'] value, then calls getResourceWildcard to set the $base variable value using the last part of the $name as the input parameter.

The Illuminate\Routing\ResourceRegistrar getResourceWildcard method will look through the parameters to see if there is a value, and use it if there is. Otherwise it goes through some gyrations to make a string.

So if we pass in a 'parameters' array with a key that matches the route name, and the value equal to the place-holder name, we can avoid the error.

The issue with getResourceWildcard was mentioned here:

https://github.com/laravel/framework/issues/1001#issuecomment-134887584

but seemed to be rebutted here as a Symphony issue?

https://github.com/laravel/framework/issues/1001#issuecomment-212518768

Hopefully, this answer helps someone else with a work around. But it has not been extensively tested, so it may still have problems.

like image 90
Josh Petitt Avatar answered Oct 13 '22 11:10

Josh Petitt


Here's a relevant discussion. The issue seems to be more directly tied to Sympfony.

https://github.com/laravel/framework/issues/1001

like image 31
Serg Chernata Avatar answered Oct 13 '22 13:10

Serg Chernata