RoutesRequests.php 中的流明 NotFoundHttpException

lumen NotFoundHttpException in RoutesRequests.php

我知道 之前有人问过这个问题。 (请在 link 上 clink,然后在重复标签下投票关闭它。)但是 none 的解决方案对我有用。

我可以毫无错误地访问 /。但是当我尝试使用 get 请求登录时,出现此错误:

NotFoundHttpException
in RoutesRequests.php (line 594)
at Application->handleDispatcherResponse(array(0))
in RoutesRequests.php (line 532)
at Application->Laravel\Lumen\Concerns\{closure}()
in RoutesRequests.php (line 781)
at Application->sendThroughPipeline(array(), object(Closure))
in RoutesRequests.php (line 534)
at Application->dispatch(null)
in RoutesRequests.php (line 475)
at Application->run()
in index.php (line 28)

index.php (line 28) 指向 $app->run();

这是我的登录表单页面代码:

<!DOCTYPE html>
<html>
    <head>
    <title>notes!</title>
    <!-- <link rel="stylesheet" type="text/css" href="{{ url('../assets/css/main.css') }}"> -->
    <style>
            body {
            background-color: #ffffff;
        }

        .banner {
            margin: 100px auto;
            text-align: center;
            vertical-align: middle;
            font-family: 'Open Sans', sans-serif;
            font-size: 30px;
            color: #333333;
        }

        .loginContainer {
            width:30%;
            margin: 50px auto;
            vertical-align: middle;
            background-color: #333333;
            border:2px;
            border-radius:10px;
        }

        form {
            margin: 0 auto;
            padding: 50px;
        }

        form input {
            display: block;
            width: 300px;
            margin: 0 auto;
            margin-top: 5px;
            padding: 5px;
            font-size: 30px;
        }

        form button {
            display: block;
            width: 314px;
            margin: 0 auto;
            margin-top: 5px;
            padding: 5px;
            border-radius: 5px;
            border: none;
            background-color: #3366ff;
            color: #ffffff;
            font-size: 30px;
        }
    </style>
    <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
    </head>
    <body>
    <!-- <div class="navbar">
         <ul>
         <li>Login</li>
         <li>About Me!</li>
         </ul>
         </div> -->
    <div class="banner">
        <h1>Welcome to notes!</h1>
        <h4>Add notes, to-do lists and more!</h4>
    </div>
    <div class="loginContainer">
        <form class="signin" method="get" action="/login/">
        <input type="email" id="inputEmail" placeholder="Email" required="" autofocus="">
        <input type="password" id="inputPassword" placeholder="Password" required="">
        <button type="submit">Sign in</button>
        </form>
    </div>
    </body>
</html>

这是 routes.php 的代码:

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/

$app->get('/', function () use ($app) {
    return view('welcome');
});

$app->get('login/','UsersController@authenticate');
$app->post('todo/','TodoController@store');
$app->get('todo/', 'TodoController@index');
$app->get('todo/{id}/', 'TodoController@show');
$app->put('todo/{id}/', 'TodoController@update');
$app->delete('todo/{id}/', 'TodoController@destroy');

?>

这是 UsersController.php 文件:

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\Hash;

use Illuminate\Http\Request;

use App\Users;

class UsersController extends Controller

{

  public function __construct()

   {

//        $this->middleware('auth:api');

   }

   /**
    * Display a listing of the resource.
    *
    * @return \Illuminate\Http\Response
    */

   public function authenticate(Request $request)

   {

       $this->validate($request, [

       'email' => 'required',

       'password' => 'required'

        ]);

      $user = Users::where('email', $request->input('email'))->first();

     if(Hash::check($request->input('password'), $user->password)){

          $apikey = base64_encode(str_random(40));

          Users::where('email', $request->input('email'))->update(['api_key' => "$apikey"]);;

          return response()->json(['status' => 'success','api_key' => $apikey]);

      }else{

          return response()->json(['status' => 'fail'],401);

      }

   }

}    

?>

路由第一个参数用'/'修剪

修复形成动作: /登录/ -> /登录

<form class="signin" method="get" action="/login">

那是因为您的 action 属性中的尾部反斜杠,但这里有几点需要考虑:

首先我不知道你为什么要使用GET request for login form since the credentials you send to the server will appear in the url in the user's browser and this kind of info should be safe especially the Password - another thing why don't you use blade引擎让你的视图更优雅但是没关系我们可以使用原生php语法。

  • 为了获得 /login 路线的正确路径,我们将使用 url

  • 然后我们将方法更改为 post 这非常适合我们想要实现的目标:将数据发送到我们的服务器而不显示在用户的浏览器中,因此我们的表单看起来像这个:

    <form class="signin" method="post" action="<?=url('/login');?>"> <input type="email" id="inputEmail" name="email" placeholder="Email"> <input type="password" id="inputPassword" name="password" placeholder="Password"> </form>

  • 另一件事我添加了 name 属性,这在检索表单值时很重要。

  • 我们还将在 routes/web.php 中使用 post 方法,如下所示:

    $app->post('/login','UsersController@authenticate');

我认为你 .htaccess 文件丢失,在 public 目录中添加 .htaccess 文件并为 apache 分配读取权限:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ / [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>