Laravel href 重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30733055/
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 11:44:31  来源:igfitidea点击:

Laravel href redirection

laravel

提问by wobsoriano

So I've started using Laravel and I found it very easy and now I'm creating my own restful services. My problem is I don't know if I am doing the href link correct, but yes it is working. Here is the code:

所以我开始使用 Laravel,我发现它非常容易,现在我正在创建自己的宁静服务。我的问题是我不知道我做的 href 链接是否正确,但是它正在工作。这是代码:

<a href="{{URL::to('accounts/create')}}">Add user</a>

And in my controller I just render the blade:

在我的控制器中,我只渲染刀片:

public function create()
{
    return view('accounts.create');
}

So if I click the link Add user, it will redirect me to localhost:8080/accounts/createwhich is working well. My question is, is there a better way of doing this? Like if ever I changed any in my routes file, I will not change anymore the href link?

因此,如果我单击“添加用户”链接,它会将我重定向到localhost:8080/accounts/create运行良好的位置。我的问题是,有没有更好的方法来做到这一点?就像我更改了路由文件中的任何内容一样,我将不再更改 href 链接?

回答by karanmhatre

Ideally, you will name the route in your routes file. Something like,

理想情况下,您将在路由文件中命名路由。就像是,

Route::get('accounts/create', [as => 'createAccount', 'uses' => 'AccountsController@create']);

You will use it as follows

您将按如下方式使用它

<a href="{{URL::route('createAccount')}}">Add user</a>

in your view.

在你看来。

This way, even if you change the url (accounts/create), or the action name (create), you will not have to change it in the view. Allows your view to be independent.

这样,即使您更改了 url (accounts/create) 或操作名称 (create),您也不必在视图中更改它。允许您的视图独立。

回答by haakym

What you can do is give your route a name using the askey in the array in the second argument of your route:

您可以做的是使用路线as的第二个参数中的数组中的键为您的路线命名:

Route::get('accounts/create', [
    'as'   => 'accounts.create',
    'uses' => 'AccountController@create'
]);

Then you can refer to this route in your application by it's name and it'll go to the same place even if you happen to change the URL. For an anchor tag you can do the following:

然后你可以在你的应用程序中通过它的名字来引用这个路由,即使你碰巧改变了 URL,它也会去同一个地方。对于锚标记,您可以执行以下操作:

{{ URL::route('accounts.create') }}

If you're using a resource controller there will be predefined routes which you can see here under Actions Handled By Resource Controller: http://laravel.com/docs/5.1/controllers#restful-resource-controllers

如果您使用的是资源控制器,则会有预定义的路由,您可以在资源控制器处理的操作下看到这些路由:http: //laravel.com/docs/5.1/controllers#restful-resource-controllers

You can always get a quick overview of your available routes and their names by running php artisan route:list

您始终可以通过运行快速了解可用路线及其名称 php artisan route:list

回答by Jesús

Yes, you can use the action() helper to call a method inside a controller and generate the route to it automatically on demand.

是的,您可以使用 action() 助手调用控制器内的方法并根据需要自动生成到它的路由。

So let's consider you have a controller called FrontendController.php and a method called showFrontend( $section), and assuming that you have a route that matches this controller and method (let's say "frontend/show/{$section}", you can call:

因此,让我们假设您有一个名为 FrontendController.php 的控制器和一个名为 showFrontend( $section) 的方法,并假设您有一个与此控制器和方法匹配的路由(假设为“frontend/show/{$section}”,您可以称呼:

action('FrontendController@showFrontend', array( 'index' ) )

That will return:

那将返回:

frontend/show/index

So basically it looks for the route associated to that method/controller. You can combine this with other helpers to create a whole URL.

所以基本上它寻找与该方法/控制器关联的路由。您可以将它与其他帮助程序结合起来创建一个完整的 URL。

NOTE: Consider the namespaces, in case that you have different folder for controllers, nested resources, etc.

注意:考虑命名空间,以防控制器、嵌套资源等有不同的文件夹。

I hope it helps!

我希望它有帮助!

回答by Bishal Paudel

http://laravel.com/docs/4.2/routing#named-routes

http://laravel.com/docs/4.2/routing#named-routes

Example:

例子:

Route::get('accounts/create', array('as' => 'signup', 'uses' => 'UserController@create'));

<a href="{{URL::route('signup')}}">Add user</a>

This route is named as "signup" and you can change the url anytime as:

这条路线被命名为“注册”,你可以随时更改网址:

Route::get('accounts/signup', array('as' => 'signup', 'uses' => 'UserController@create'));