【问题标题】:link to specific anchor on a page with Laravel使用 Laravel 链接到页面上的特定锚点
【发布时间】:2014-08-08 03:58:30
【问题描述】:

如何使用 Laravel 链接到视图中的特定 id?

我的控制器:

public function services() {
 return View::make('services.custom_paint', array('pageTitle' => 'Custom Paint'))->with('default_title', $this->default_title);
}

我的路线:

Route::get('/custom_paint',  'PagesController@services');

我尝试将#id 添加到路由、控制器,甚至视图的URL::to。似乎没有任何效果。

我认为只需将 id 添加到 URI 就可以了,但显然我遗漏了一些东西。

【问题讨论】:

  • 您能否展示一些您尝试过的代码“将#id 添加到路由、控制器甚至视图的 URL::to”?

标签: php laravel blade laravel-routing


【解决方案1】:
// Controller
Route::get('/custom_paint', array('as' => 'custom_paint', 'uses' => 'PagesController@services'));

// View
<a href="{{ URL::route('custom_paint') }}#id">LINK</a>

试试这个代码 ;) 希望它能工作..

【讨论】:

  • 在控制器中,这也可以使用(至少在 Laravel 5.5 中):return redirect()-&gt;to(route('custom_paint') . '#id');(另见:stackoverflow.com/a/41606730/2286722)
  • @lieroes 我试过你的代码,但在我的情况下不起作用,你能看看我的问题吗? stackoverflow.com/q/62847193/12030116,如果这个问题得到解决,我会支持你的两个答案(在我的问题和这个问题中)。
【解决方案2】:

如果您将 id 作为参数传递给控制器​​,并且需要使用 #id 指向正确的结果

 <a href="{{ route('custom_paint',['id'=>'$id_value','#id']) }}">Try</a>

我假设上面将呈现为http://localhost/custom_paint/id=123#id

或者直接指向ID:

<a href="{{ route('custom_paint',['#id']) }}">Try</a>

我假设上面将呈现为http://localhost/custom_paint/#id

【讨论】:

    【解决方案3】:

    简单地使用

    &lt;a href="{{ route('custom_paint') }}"&gt;LINK&lt;/a&gt;

    如果需要参数就这样使用

    &lt;a href="{{ route('custom_paint', 'param1') }}"&gt;LINK&lt;/a&gt;

    route

    【讨论】:

      猜你喜欢
      • 2014-05-24
      • 1970-01-01
      • 1970-01-01
      • 2013-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-08
      相关资源
      最近更新 更多