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

Question

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

Answers 2
Added at 2016-12-27 18:12
Tags
Question

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'
);
Answers
nr: #1 dodano: 2016-12-27 18:12

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

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

nr: #2 dodano: 2016-12-27 18:12

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.

Source Show
◀ Wstecz