简体   繁体   中英

Laravel Router Draws in URL Encoding from View, Thus URL Encoding Model Attribute

Original route went to /{id}, and I changed the route to /{name}

In the view, I iterate over the particular model, leading to this blade html:

<a href="{{ route('modelHistory', urlencode(strtolower($model->name)) ) }}">
                                  {{$model->name}}
                                </a>

And then I had some code in the Controller to reconvert the name passed to the route into a name that matches what's in the database.

Laravel, somehow, sucked the urlencode and strtolower code into itself, possibly the middleware, such that any retrieval of Model::all gave us a name that was urlencoded and all lowercase.

It became an issue, because I wanted to change URL to be the name converted to lowercase with underscores instead of urlencoded, but the names remained urlencoded and lowercased, which was a problem when another view lists all instances of Model and surfaces the name attribute.

Solution below.

After changing the str functions in the view, I went to the command line and typed in php artisan . One of the options was php artisan route:clear which clears the route cache. I also did php artisan view:clear , but I believe it was the route clearing that did it.

And suddenly, the names were normal again. I then used the string functions I wanted, and Laravel hasn't sucked them in and applied them all the names in Model:all() .

For the record, here's the final setup:

Route::get('history/{name}', 'Model0\Model1sController@getHistory')->name('model1History');

<a href="{{ route('model1History', preg_replace('/(\s|\+)/i', '_', strtolower($model1->name)) ) }}">
   {{$model1->name}}
</a>

This was a frustrating experience, so hopefully you all don't have to deal with it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM