Laravel Put Route Giving 404 错误

Posted

技术标签:

【中文标题】Laravel Put Route Giving 404 错误【英文标题】:Laravel Put Route Giving 404 Error 【发布时间】:2012-12-31 13:36:00 【问题描述】:

所以我一直在尝试在路由中使用“put”方法,我直接从“routes.php”文件中遵循了这个示例:

Route::put('hello/(:any)', function($name)

      return "Welcome, $name.";
);

这会返回 404 错误,我真的希望我的代码如下所示。我希望能够从 url 获取参数并使用它们进行验证,稍后我计划使用一种编码方法,但现在我只想让路由正常工作,然后再尝试对它们进行编码。任何帮助将不胜感激!!!

Route::put('admin/activate/(:any?)/(:any?)', function($r, $e) 
     return "Random value: $r <br/> Email: $e";
);

您可以在此处找到该页面:http://diverseevolution.com/admin/activate/randomstring/test@gmail.com

这是我的整体“routes.php”文件:

Route::get('/', function() 
     return View::make('homepage.index');
);

///////////////////////////////////////////////////////////////////////////////////////////
///////////////        Administration Account Creation & Login        /////////////////////
///////////////////////////////////////////////////////////////////////////////////////////

// Create account
Route::get('admin/createform', function() 
     return View::make('admin.createform');
);

// action for actual admin creation
Route::post('admin/create', array('before' => 'csrf', function() 
     $rules = array(
          'fname' => 'required|min:3',
          'lname' => 'required|min:3',
          'email' => 'required|email|unique:users',
          'pword' => 'required'
     );

     $validated = Validator::make(Input::all(), $rules);

     if($validated -> fails()) 
          return Redirect::to('admin/createform')->with_input();
      else 
          $r = Hash::make(time() .'ReAl19dk4-^4$'. $_POST['pword']);

          $user = new User;

          $user -> fname = $_POST['fname'];
          $user -> lname = $_POST['lname'];
          $user -> email = $_POST['email'];
          $user -> pword = Hash::make($_POST['pword']);
          $user -> status = 'unactivated';
          $user -> random = $r;

          if($user -> save()) 

               //////////////////////// Email /////////////////////////////////
               // We still need to make this functionality

               $msg = '
               <h1>Your admin account has been created!</h1>
               <p>Congratulations on creating your new account, there is one last step before your account can be activated. Below is a link, simply follow the link to activate your account, once you have your account will be active and you will be able to login!</p>
               <p><a href="http://diverseevolution.com/admin/activate/'. $r .'/'. $_POST['email'] .'">http://diverseevolution.com/admin/activate/'. $r .'/'. $_POST['email'] .'</a></p>
               <p>Thanks, Diverse Evolution</p>
               ';

                    // Mail headers
                    $headers  = "Reply-To: Diverse Evolution <info@diverseevolution.com>\r\n"; 
                    $headers .= "Return-Path: Diverse Evolution <info@diverseevolution.com>\r\n"; 
                    $headers .= "From: Diverse Evolution <info@diverseevolution.com>\r\n"; 
                    $headers .= "Organization: Diverse Evolution\r\n";
                    $headers .= 'MIME-Version: 1.0' . "\r\n";
                    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                    $headers .= "X-Priority: 3\r\n";
                    $headers .= "X-Mailer: PHP". phpversion() ."\r\n";
                    define('_headers', $headers);

               if(mail($_POST['email'], 'Diverse Evolution Account Created', $msg, _headers)) 
                    return Redirect::to('admin/thanks');
                else 

               
           else 
               return Redirect::to('admin/createform')->with_input();
          

     
));

// creates the thank you page for the admin account creation
Route::get('admin/thanks', function() 
     return View::make('admin/thanks');
);

// account activation email, this is still not working 011613
Route::put('admin/activate/(:any?)/(:any?)', function($r, $e) 
     return "Random value: $r <br/> Email: $e";
);

// Login form
Route::get('admin/loginform', function() 
     return View::make('admin/loginform');
);

// Login action
Route::post('admin/login', array('before' => 'csrf', function() 
     $rules = array(
          'email' => 'required|email',
          'pword' => 'required'
     );

     $validated = Validator::make(Input::all(), $rules);

     if($validated -> fails()) 
          return Redirect::to('admin/loginform')->with_input();
      else 
          $credentials = array('username' => $_POST['email'], 'password' => $_POST['pword']);

          if (Auth::attempt($credentials)) 
               return Redirect::to('admin/dash');
           else 
               return Redirect::to('admin/loginform')->with_input();
          
     
));

Route::get('admin/logout', function() 
     Auth::logout();
     return Redirect::to('admin/loginform');
);

///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////        Administration Pages        ////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
Route::group(array('before' => 'auth'), function() 
    Route::get('admin/dash', function() 
        return 'Dashboard';
    );
);















/*
|--------------------------------------------------------------------------
| Application 404 & 500 Error Handlers
|--------------------------------------------------------------------------
|
| To centralize and simplify 404 handling, Laravel uses an awesome event
| system to retrieve the response. Feel free to modify this function to
| your tastes and the needs of your application.
|
| Similarly, we use an event to handle the display of 500 level errors
| within the application. These errors are fired when there is an
| uncaught exception thrown in the application.
|
*/

Event::listen('404', function()

    return Response::error('404');
);

Event::listen('500', function()

    return Response::error('500');
);

/*
|--------------------------------------------------------------------------
| Route Filters
|--------------------------------------------------------------------------
|
| Filters provide a convenient method for attaching functionality to your
| routes. The built-in before and after filters are called before and
| after every request to your application, and you may even create
| other filters that can be attached to individual routes.
|
| Let's walk through an example...
|
| First, define a filter:
|
|       Route::filter('filter', function()
|       
|           return 'Filtered!';
|       );
|
| Next, attach the filter to a route:
|
|       Route::get('/', array('before' => 'filter', function()
|       
|           return 'Hello World!';
|       ));
|
*/

Route::filter('before', function()

    // Do stuff before every request to your application...
);

Route::filter('after', function($response)

    // Do stuff after every request to your application...
);

Route::filter('csrf', function()

    if (Request::forged()) return Response::error('500');
);

Route::filter('auth', function()

    if (Auth::guest()) return Redirect::to('admin/loginform');
);

【问题讨论】:

【参考方案1】:

使用 GET 有效吗?

Route::put('hello/(:all)', function($name)

      return "Welcome, $name.";
);

都是因为@

【讨论】:

【参考方案2】:

您没有包含表单 - 但我猜您没有在表单中包含“PUT”。

echo Form::open('user/profile', 'PUT');

You can see more about forms and PUT here。但一般来说,您需要在表单中专门包含 PUT,否则它将只是 POST(浏览器默认)。

【讨论】:

感谢您的回答有效,我没有意识到您需要以我认为 put 是框架的一部分并且不知道浏览器有问题的形式指定它!

以上是关于Laravel Put Route Giving 404 错误的主要内容,如果未能解决你的问题,请参考以下文章

laravel 路由器

Laravel之路由

laravel向我显示此错误此路由不支持POST方法。支持的方法:GET,HEAD,PUT,DELETE

此路由不支持 PUT 方法。 Vue/laravel

laravel中的表单请求类型和CSRF防护

当 PUT 通过 AJAX 用于 Laravel 资源时,方法不允许