Laravel redefines route and takes wrong

I have it defined in routes.php file

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

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

      

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

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

      

I don't know why, when I submit the form, the second route is selected instead of the second, although I am pointing to the first.

There must be a way to navigate to the same URL from two different forms, which is what I want.

+2


source to share


3 answers


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

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

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

      

How can Laravel know the difference between the two when something is horrible /gestionAdministrador

?

He will always count first.

The name you set 'as' => 'RegistrarAdministrador'

will be used to generate URLs based on this route name, only when the URL (browser, curl ...) hits the URL, the only ways to differentiate them are

1) URL

2) URL parameters (which basically contains the number 1 plus)

3) Method (GET, POST)

So, you can 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'));

      

EDIT 2

What you really need to understand is that the name you point to the route ('as' => 'name') will not be part of your url, so this is not something Laravel can use to differentiate your two URls, this is for internal use only, to define your routes when generating urls. So these instructions:



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

      

Would create exactly the same url:

http://yourserver.dev/gestionAdministrador

      

EDIT 1 - REPLY TO COMMENT

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

return Redirect::to('/');

      

or

return Redirect::route('home');

      

Having a route like this:

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

      

So your controller will look like this:

class AdministradorController extends Controller {

    public function RegistrarAdministrador() 
    {
        ...

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

    public function Login() 
    {
        ...

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

      

+3


source


You actually only have one route in your route collection because:

You have declared the following routes:

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

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

      

Both of them use a method post

, and this is a method post

:

public function post($uri, $action)
{
    return $this->addRoute('POST', $uri, $action);
}

      

It calls addRoute

, and here it is:

protected function addRoute($methods, $uri, $action)
{
    return $this->routes->add($this->createRoute($methods, $uri, $action));
}

      

Here $this->routes->add

means Illuminate\Routing\RouteCollection::add()

, and the method add()

calls addToCollections()

, and it looks like this:



protected function addToCollections($route)
{
    foreach ($route->methods() as $method)
    {
        $this->routes[$method][$route->domain().$route->getUri()] = $route;
    }

    $this->allRoutes[$method.$route->domain().$route->getUri()] = $route;
}

      

$routes

is an array ( protected $routes = array();

) and it is obvious that routes are grouped by methods

(GET / POST etc.) and only one unique can be accessed in each method URL

because it is something like this

$routes['post']['someUrl'] = 'a route';
$routes['post']['someUrl'] = 'a route';

      

So, in your case, the latter replaces the first, in which case you can use different methods to declare two routes using the same URL

, so it will be in a different array, something like this:

$routes['post']['someUrl'] = 'a route';
$routes['put']['someUrl'] = 'a route'; // Route::put(...)

      

There must be a way to navigate to the same URL from two different forms

Yes, there is a way, and simply, that you have to use the same route as your form action, and therefore you don't have to declare it twice.

+3


source


What you want to do is a bad idea, you shouldn't register and register from the same route. With this said, what you say is actually impossible. Routing in Laravel is first done first. Basically it checks the route until the URI matches one and then calls that method on the controller or does a callback. Your routes should be in a different way in your routes file. This will be fixed by changing the url.

+1


source







All Articles