Laravel overrides named route and takes wrong one

前端 未结 3 945
春和景丽
春和景丽 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:20

    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'));
    

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

    It will always assume the first one.

    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) URL

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

    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'));
    

    EDIT 2

    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');
    

    Would generate exactly the same URL:

    http://yourserver.dev/gestionAdministrador
    

    EDIT 1 - TO ANSWER A COMMENT

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

    return Redirect::to('/');
    

    or

    return Redirect::route('home');
    

    Having a route like this one:

    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');
        }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-23 05:43

    What you want to do is a bad idea, you shouldn't be logging in and registering from the same route. With that said what you are saying isn't really possible. Routing in Laravel is first come first served. Basically it checks the route until the URI matches one and then calls that method on the controller or executes the callback. Your routes have to be the other way in your routes file. This will be fixed by changing the url.

    0 讨论(0)
提交回复
热议问题