0

I have a standard resource controller registered like this:

Route::resource('/movies', \App\Http\Controllers\MovieController::class );

I checked php artisan route:list and I have the get 'movies' route.

But in blade template

<a href="{{ route('movies') }}" class="hover:text-gray-300">Movies</a>

does not work anymore.

And only

<a href="{{ url('movies') }}" class="hover:text-gray-300">Movies</a>

works.

Before that when I had:

Route::get('/movies', function () {
    return view('movies');
})->name('movies');

The route method worked.

So url() method works everywhere and route() only in specific cases.

Could anyone tell me what is the main difference between those two methods?

which one should be used when?

And the main question - is it OK that the route() method does not work with resource controller, or is it something wrong with my code?

And since url() works everywhere should I just forget about route() method as unreliable?

Ahmad
  • 274
  • 2
  • 15
IVN
  • 241
  • 1
  • 3
  • 13
  • 1
    try php artisan route:list to see all the route names registered – Anurat Chapanond Jan 02 '21 at 09:03
  • 1
    route() will look for route name, url() will be just query string – Anurat Chapanond Jan 02 '21 at 09:05
  • thanks, @AnuratChapanond but I wrote in my question that I checked php artisan route:list it and I have get 'movies' route. – IVN Jan 02 '21 at 09:06
  • @AnuratChapanond what do you mean by saying "just query string"? (I know what query string means in general, but in this case?) – IVN Jan 02 '21 at 09:08
  • 1
    if you print out php artisan route:list there will be URL column and Name column, basically URI column goes into url() and Name column goes into route() – Anurat Chapanond Jan 02 '21 at 09:09
  • 1
    GET /movies route may have url('/movies') and route('movies.index') – Anurat Chapanond Jan 02 '21 at 09:11
  • Wow! thank you very much... I did not read the list correctly now it works.. The name of the route turned out is 'movies.index'... You can post an answer and I will immediately accept it... Cheers! – IVN Jan 02 '21 at 09:14
  • Does this answer your question? [Laravel named route for resource controller](https://stackoverflow.com/questions/25290229/laravel-named-route-for-resource-controller) – Remy Jan 02 '21 at 09:23

1 Answers1

1

If you look at the result of php artisan route:list you will see several routes e.g.

|        | POST      | movies                     | movies.store        | App\Http\Controllers\MovieController@store        | web        |
|        | GET|HEAD  | movies                     | movies.index        | App\Http\Controllers\MovieController@index        | web        |
|        | DELETE    | movies/{movie}            | movies.destroy      | App\Http\Controllers\MovieController@destroy      | web        |
|        | PUT|PATCH | movies/{movie}            | movies.update       | App\Http\Controllers\MovieController@update       |

route() and url() accept different values.
route() will accept route names e.g. movies.index, movies.destroy, movies.update and
url() will accept movies, movies/23.

route() will select the right methods (GET, POST, PATCH, DELETE) for you but
url() will only provide the path.

Anurat Chapanond
  • 2,837
  • 3
  • 18
  • 31