Right way to build a link in laravel 5.3

一笑奈何 提交于 2019-12-01 00:42:29

You can use named routes for this

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

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

Much more cleaner IMO

You can use named routes for cleaner in code

In your app/Http/routes.php (In case of laravel 5, laravel 5.1, laravel 5.2) or app/routes/web.php (In case of laravel 5.3)

Define route

Route::get('articles/{id}/edit',[
             'as'   =>'articles.edit',
             'uses' =>'YourController@yourMethod'
            ]);

In Your view page (blade) use

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

One benefits of using named routes is if you change the url of route in future then you don't need to change the href in view (in your case)

You can try with this

<a href="{{ url('/articles/edit',$article->id) }}"><i class="fa fa-fw fa-edit"></i></a>

and your route.php file

Route::get('/articles/edit/{art_id}', 'ArticlesController@edit');

I recommend to work with named routes!

Your routes/web.php file:

Route::get('articles/{articleId}/edit', 'YourController@action')->name('article.edit');

Your Blade-Template file:

<a href=" {{ route('article.edit', ['articleId' => $article->id]) }}">Edit></a>  
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!