Laravel覆盖命名的路线并采取错误的路线 [英] Laravel overrides named route and takes wrong one

查看:72
本文介绍了Laravel覆盖命名的路线并采取错误的路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的route.php文件中定义了

I have this defined in my routes.php file

Route::post('gestionAdministrador', array('as' => 'Loguearse', 'uses' => 'AdministradorController@Login'));

Route::post('gestionAdministrador', array('as' => 'RegistrarAdministrador', 'uses' => 'AdministradorController@RegistrarAdministrador'));

在我的login.blade.php文件中,表单以此形式开始

And in my login.blade.php file, the form starts as this

{{ Form::open(array('route'=>'Loguearse'))}}

我不知道为什么我提交表单时会采用第二条路线而不是第一条路线,即使我指向的是第一条路线.

I dont know why when i submit the form takes the second route instead the first one, even though I am pointing to the first one.

必须要有一种方法,可以从两种不同的形式访问相同的url,这正是我想要的.

There must be a way to go to the same url from two different forms, that is what I want.

推荐答案

如果您有两条具有完全相同的URI和相同方法的路由:

If you have two routes with the exact same URI and same method:

Route::post('gestionAdministrador', array('as' => 'Loguearse', 'uses' => 'AdministradorController@Login'));

Route::post('gestionAdministrador', array('as' => 'RegistrarAdministrador', 'uses' => 'AdministradorController@RegistrarAdministrador'));

当某物击中/gestionAdministrador时,Laravel如何知道它们之间的区别?

How can Laravel know the difference between them when something hit /gestionAdministrador?

它将始终采用第一个.

您设置的名称'as' => 'RegistrarAdministrador'仅用于基于该路由名称创建URL,仅当某些内容(浏览器,curl ...)命中URL时,区分它们的唯一方法是

The name you set 'as' => 'RegistrarAdministrador' will be used to create URLs based on that route name, only, when something (browser, curl...) hit the URL the only ways to differentiate them is by

1)网址

2)URL参数(基本上是1号加参数)

2) URL parameters (which is basically number 1 plus parameters)

3)方法(GET,POST)

3) Method (GET, POST)

因此您可以将它们更改为:

So you could change them to something like:

Route::post('gestionAdministrador/loguearse', array('as' => 'Loguearse', 'uses' => 'AdministradorController@Login'));

Route::post('gestionAdministrador/registrar', array('as' => 'RegistrarAdministrador', 'uses' => 'AdministradorController@RegistrarAdministrador'));

编辑2

您真正需要了解的是,您给路由提供的名称('as'=>'name')将不是您的网址的一部分,因此Laravel不能用它来区分两个URls ,仅供内部使用,以在创建URL期间标识您的路由.因此,这些说明:

What you really need to understand is that the name you give to a route ('as' => 'name') will not be part of your url, so this is not something that Laravel can use to differentiate your two URls, this is for internal use only, to identify your routes during the creation of URLs. So, those instructions:

$loguearse = URL::route('Loguearse');
$registrar = URL::route('RegistrarAdministrador');

将生成完全相同的URL:

Would generate exactly the same URL:

http://yourserver.dev/gestionAdministrador

编辑1-回答评论

在Laravel中进行重定向很容易,在您的控制器中,在处理完表单后,您可以通过任何一种方法进行操作:

Redirecting in Laravel is easy, in your controller, after processing your form, in any of your methods you can just:

return Redirect::to('/');

return Redirect::route('home');

具有这样的路线:

Route::get('/', array('as' => 'home', 'uses' => 'HomeController@index'));

因此,您的控制器将如下所示:

So, your controller would look like this:

class AdministradorController extends Controller {

    public function RegistrarAdministrador() 
    {
        ...

        return Redirect::route('home');
    }

    public function Login() 
    {
        ...

        return Redirect::route('home');
    }
}

这篇关于Laravel覆盖命名的路线并采取错误的路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆