【问题标题】:Laravel post route controller method not getting calledLaravel 后路由控制器方法没有被调用
【发布时间】:2016-06-28 14:19:52
【问题描述】:

我正在 Laravel 中构建一个应用程序,并且我的用户注册工作正常,我现在想修改注册的工作方式,所以我修改了我的路由以调用不同的方法。

原来路由是这样定义的:

Route::post('register', 'Auth\AuthController@postRegister');

这很好用。然后我将其更改为:

Route::post('register', 'Auth\AuthController@someOtherMethod'); 

该方法定义如下:

public function someOtherMethod(Request $request)
{
    die('If the method is called you should see this');
}

但是,它没有被调用(消息没有显示)。相反,它会将我重定向到网站的根目录。

请注意,我的服务器上有一个缓存清除脚本,每次遇到类似这样的奇怪问题时都会运行该脚本,该脚本运行以下命令:

php artisan route:clear
php artisan cache:clear
service php5-fpm restart
service nginx restart

每次我进行更改时,我都会在隐身/私人窗口中运行该页面。

现在是有趣的部分;我尝试撤消所做的更改,以便它再次调用postRegister,我完全希望这会使其恢复为默认行为,但它仍然会将我重定向到站点的根目录!所以现在我什至连一个注册页面都没有。

有人知道发生了什么吗?

提前感谢您的帮助。

编辑:

这是我完整的routes.php

use Illuminate\Http\Request;

Route::group(['middleware' => 'web'], function () {
    /** Public routes **/
    Route::get('',                              'HomepageController@index');
    Route::get('/',                             'HomepageController@index');
    Route::get('terms', function() {
        return view('terms');
    });
    Route::get('privacy', function() {
        return view('privacy');
    });

    /** Public auth routes **/
    Route::get('register',                      'RegistrationController@index');
    Route::post('register',                     'Auth\AuthController@postRegister');
    Route::get('login', function() {
        return view('auth.login');
    });
    Route::post('login',                        'Auth\AuthController@postLogin');
    Route::get('logout',                        'Auth\AuthController@getLogout');
    Route::get('dashboard/login', function() {
        return view('admin.login');
    });
    Route::post('dashboard/login',              'AdminAuth\AuthController@postLogin');
    Route::get('dashboard/logout',              'AdminAuth\AuthController@getLogout');

    /** Admin routes **/
    Route::get('dashboard', [
      'middleware' => 'admin',
      'uses' => 'Admin\DashboardController@index'
    ]);
    Route::get('dashboard/users', [
      'middleware' => 'admin',
      'uses' => 'Admin\DashboardController@showUsers'
    ]);
    Route::get('dashboard/search', [
        'middleware' => 'admin',
        'as' =>   'adminSearch',
        'uses' => 'Admin\DashboardController@query'
    ]);

    /** Admin auth routes **/
    Route::get('dashboard/staff/create', [
        'middleware' => 'admin',
        function () {
            return view('admin.register');
        }
    ]);
    Route::post('dashboard/staff/create', [
        'middleware' => 'admin',
        'uses' => 'AdminAuth\AuthController@postRegister'
    ]);

    /** Controllers **/
    Route::controllers([
        'password' =>                           'Auth\PasswordController',
    ]);
});

【问题讨论】:

  • 点击您的注销网址并重试?
  • 是的,我也每次都这样做。此外,正如我提到的,我每次尝试此操作时都会打开一个新的隐身/私人窗口,因此登录甚至不应该成为问题。
  • 你把这条路线放在哪里了?在web 中间件之外。对吗?
  • @Denis 给什么?目前它设置为{{ url('/register') }},这就是路由所指向的。
  • 尝试从RegisterController@postRegister调用方法

标签: php laravel laravel-routing laravel-5.2


【解决方案1】:

确保您的 config/auth.php 调用了正确的 Auth 保护。此外,redirectsTo 默认设置为“/”,即站点路径。你的中间件里有什么?默认情况下,RedirectIfAuthenticated 中间件具有指向站点根目录的默认条目。只有这 3 种情况可能与预期不同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-12
    • 2015-02-28
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    相关资源
    最近更新 更多