douwen5246 2014-01-08 00:50
浏览 38
已采纳

如何在我的视图中引用Laravel路由,这样如果我更改路由,我将不必更新所有视图?

Consider the following simple example:

// routes.php 

  // You can get this route by http://localhost/adminpanel/home ...users...etc
  Route::group(array('prefix' => 'adminpanel', 'before' => 'auth'), function(){
      Route::resource('home', 'Admin\Controllers\HomeController');
      Route::resource('users', 'Admin\Controllers\UsersController');
  });


// Admin\Views\users.php
  <!-- I do not want to update this everytime I change the route -->
  <a href="<?=URL::to('adminpanel/home');?>">Click here to go home</a>

If I change adminpanel to something else in my routes.php I will also have to change every View that used it.

Is there another way I can reference the route in my Views or is there any other good strategy I can implement from Laravel naturally?
Otherwise I'm thinking I might need to add some sort of CONSTANT variable to my global.php to possibly accomplish this.

  • 写回答

2条回答 默认 最新

  • doujiena0025 2014-01-08 00:53
    关注

    Use the route 'as' parameter to give a name to your route:

    Route::get('user/profile', array('as' => 'profile', 'uses' => 'UserController@showProfile'));
    

    Then you just have to

    <a href="<?=URL::route('profile');?>">Click here to go home</a>
    

    Using your resource routes, you can list them

    php artisan routes
    

    And use the name of the route, third column.

    Using your example, you'll have 2 base routes: home and users, this is the list for the home resources:

    +--------+----------------------+--------------+------------------------------------------+----------------+---------------+
    | Domain | URI                  | Name         | Action                                   | Before Filters | After Filters |
    +--------+----------------------+--------------+------------------------------------------+----------------+---------------+
    |        | GET home             | home.index   | Admin\Controllers\HomeController@index   |                |               |
    |        | GET home/create      | home.create  | Admin\Controllers\HomeController@create  |                |               |
    |        | POST home            | home.store   | Admin\Controllers\HomeController@store   |                |               |
    |        | GET home/{home}      | home.show    | Admin\Controllers\HomeController@show    |                |               |
    |        | GET home/{home}/edit | home.edit    | Admin\Controllers\HomeController@edit    |                |               |
    |        | PUT home/{home}      | home.update  | Admin\Controllers\HomeController@update  |                |               |
    |        | PATCH home/{home}    |              | Admin\Controllers\HomeController@update  |                |               |
    |        | DELETE home/{home}   | home.destroy | Admin\Controllers\HomeController@destroy |                |               |
    +--------+----------------------+--------------+------------------------------------------+----------------+---------------+
    

    So you'll build that route using:

    <a href="<?=URL::route('home.index');?>">Click here to go home</a>
    

    Note that the your group prefix adminpanel won't be added to your routes names and you don't have to use it. What you need to create your routes is the exact name the command artisan routes shows in Name column.

    Using Blade you can just do

    {{ link_to_route('home.index', 'Click here to go home') }}
    

    Much cleaner, right?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大