rysrys's avatar

Named route doesn't work

Hi,

I have created routing, which I called 'saveObject' but it doesnt work.

the url i am reciving is: /admin/routes.saveobject

and I would like it to be: /admin/saveobject

how can I fix this?

here is routing from web.php

Route::match(['GET', 'POST'], trans('routes.saveobject').'/{id?}','BackendController@saveObject')->name('saveObject')->middleware('CheckOwner');

and the view file

<li><a href="{{ route('saveObject') }}">Add a new tourist object</a></li>
0 likes
6 replies
tomasosho's avatar
Level 4

your web.php should be like

trans('saveobject').'/{id?}','BackendController@saveObject')->name('saveObject')->middleware('CheckOwner');
1 like
Sinnbeck's avatar

As far as I know you cannot use variables in routes like that I'm afraid (how would you cache it?)

Try this instead

Route::match(['GET', 'POST'], '{trans}, /{id?}','BackendController@saveObject')->name('saveObject')->middleware('CheckOwner');

<li><a href="{{ route('saveObject', ['trans' => trans('routes.saveobject')) }}">Add a new tourist object</a></li>
1 like
rysrys's avatar

I have just started learning programming, I don't quite understand which variable should I not use?

rysrys's avatar

Thank you, your solution works, but here is one thing I dont understand. I have another routes witch are very similar and works fine

Route::get(trans('routes.object').'/{id}','FrontendController@object')->name('object');
Route::post(trans('routes.roomsearch'),'FrontendController@roomsearch')->name('roomSearch');
Route::get(trans('routes.room').'/{id}','FrontendController@room')->name('room');
Route::get(trans('routes.article').'/{id}','FrontendController@article')->name('article');
Route::get(trans('routes.person').'/{id}','FrontendController@person')->name('person');

rysrys's avatar

Ok i found bug - the problem was in the routes.php file in the lang directory. It was that I forgot about case sensitivity.

here is routes.php code

<?php

    return [

       
      
        'saveObject' => 'zapiszobiekt',

      
    ]

?>

should be like this:

<?php

    return [

       
      
        'saveobject' => 'zapiszobiekt',

      
    ]

?>

Please or to participate in this conversation.