JahStation JahStation - 2 months ago 40
PHP Question

Right way to build a link in laravel 5.3

Im trying to build a dynamic link with a view page (blade) with Laravel 5.3.

My approach is:

<a href=" {{ URL::to('articles') }}/{{ $article->id}}/edit">Edit></a>


that will output the right url with my base url and some other slug:
http://mydomain/articles/23/edit


Where "23" is my article's id.

This works but I wonder if there is a cleaner way to do that?

many thanks

Answer

You can use named routes for this

// Your route file
URL::get('articles/{articleId}/edit', 'YourController@action')->name('articles.edit');

//Your view
<a href="{{ URL::route('articles.edit', $article->id) }}">Edit</a>

Much more cleaner IMO

Comments