使用 Laravel 链接到页面上的特定锚点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24275667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:39:58  来源:igfitidea点击:

link to specific anchor on a page with Laravel

phplaravelbladelaravel-routing

提问by kash101

How does one go about linking to a specific id in a view with Laravel?

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

My controller:

我的控制器:

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

My route:

我的路线:

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

I have tried to add #idto the route, to the controller, and even to the view's URL::to. Nothing seems to work.

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

I assumed that simply adding the id on to the URI would do the trick, but clearly I am missing something.

我认为简单地将 id 添加到 URI 就可以解决问题,但显然我遗漏了一些东西。

回答by lieroes

// Controller
Route::get('/custom_paint', array('as' => 'custom_paint', 'uses' => 'PagesController@services'));

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

Try this code ;) hope it works..

试试这个代码 ;) 希望它有效..

回答by Bikal Basnet

Simply use

只需使用

<a href="{{ route('custom_paint') }}">LINK</a>

<a href="{{ route('custom_paint') }}">LINK</a>

if parameter is needed use it like this

如果需要参数,请像这样使用它

<a href="{{ route('custom_paint', 'param1') }}">LINK</a>

<a href="{{ route('custom_paint', 'param1') }}">LINK</a>

route

路线

回答by Elshan

If you pass id to controller as parameter and need to point to correct result with #id

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

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

I assume above will render as http://localhost/custom_paint/id=123#id

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

Or Just point to ID:

或者直接指向 ID:

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

I assume above will render as http://localhost/custom_paint/#id

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