2016-09-22 12:56
浏览 25

在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

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dongping1922 2016-09-22 13:14

    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

    解决 无用
    打赏 举报
  • dongshi1188 2016-09-22 13:01

    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');

    解决 无用
    打赏 举报
  • duancan5327 2016-09-22 13:53

    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>  
    解决 无用
    打赏 举报
  • dongwu4834 2016-09-23 19:59

    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

                 '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)

    解决 无用
    打赏 举报

相关推荐 更多相似问题