Laravel overrides named route and takes wrong one

前端 未结 3 944
春和景丽
春和景丽 2021-01-23 05:07

I have this defined in my routes.php file

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


        
3条回答
  •  深忆病人
    2021-01-23 05:38

    Actually you have only one route in your route collection, because:

    You have following routes declared:

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

    Both of these used post method and this is post method:

    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 add() method calls addToCollections() and it is as follows:

    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;
    }
    

    The $routes is an array (protected $routes = array();) and it's obvious that routes are grouped by methods (GET/POST etc) and in each method only one unique URL could be available because it's something like this:

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

    So, in your case, the last one is replacing the first one and in this case you may use different methods to declare two routes using same URL so it would be in different array, something like this:

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

    There must be a way to go to the same url from two different forms

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

提交回复
热议问题