【问题标题】:NotFoundHttpException in RouteCollection in LaravelLaravel RouteCollection 中的 NotFoundHttpException
【发布时间】:2016-11-21 19:21:01
【问题描述】:

首先,对于大多数人来说,如果我自己还没有做一些研究,我不会在这里问,我看到很多类似的标题问题,但它们似乎与我遇到的问题不同。

=实际开始=

所以我正在关注这个名为 Laravel 5.2 PHP 构建社交网络的 Web 教程系列,但我被困在第三集的结尾。我的问题是,当我尝试单击 注册按钮 时,出现此错误:

1/1
NotFoundHttpException in RouteCollection.php line 161:
in RouteCollection.php line 161
at RouteCollection->match(object(Request)) in Router.php line 755
at Router->findRoute(object(Request)) in Router.php line 610
at Router->dispatchToRoute(object(Request)) in Router.php line 596
at Router->dispatch(object(Request)) in Kernel.php line 267
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 54

我尝试修复 web.phpwelcome.blade.phpUserController.php

谁能帮我理解问题出在哪里?

web.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::post('/signup', [
    'uses' => 'UserController@postSignUp',
    'as' => 'signup'
]);

UserController.php

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function postSignUp(Request $request){
        $email = $request['email'];
        $first_name = $request['first_name'];
        $password = bcrypt($request['password']);

        $user = new User();

        $user->email = $email;
        $user->first_name = $first_name;
        $user->password = $password;

        $user->save();

        return redirect()->back();
    }

    public function postSignIn(Request $request){
        $email = $request['email'];
        $password = $request['password'];
    }
}

welcome.blade.php

@extends('layouts.master')

@section('title')
    Welcome!
@endsection

@section('content')
    <div class="row">
        <div class="col-md-6">
            <h3>Sign Up</h3>
            <form action="{{route('signup')}}" method="POST">
                <div class="form-group">
                    <label for="email">Your Email</label>
                    <input type="form-control" type="text" name="email" id="email">
                </div>
                <div class="form-group">
                    <label for="first_name">Your First Name</label>
                    <input type="form-control" type="text" name="first_name" id="first_name">
                </div>
                <div class="form-group">
                    <label for="password">Your Password</label>
                    <input type="form-control" type="password" name="password" id="password">
                </div>
                <button type="submit" class="btn btn-primary"> Submit</button>
                <input type="hidden" name="_token" value="{{Session::token()}}">
            </form>
        </div>
        <div class="col-md-6">
            <h3>Sign In</h3>
            <form action="#" method="post">
                <div class="form-group">
                    <label for="email">Your Email</label>
                    <input type="form-control" type="text" name="email" id="email">
                </div>
                <div class="form-group">
                    <label for="password">Your Password</label>
                    <input type="form-control" type="password" name="password" id="password">
                </div>
                <button type="submit" class="btn btn-primary"> Submit</button>
            </form>
        </div>
    </div>
@endsection

我跑的时候去这个:php artisan route:list

+--------+----------+----------+--------+------------------------------------------------+----------
----+
| Domain | Method   | URI      | Name   | Action                                         | Middlewar
e   |
+--------+----------+----------+--------+------------------------------------------------+----------
----+
|        | GET|HEAD | /        |        | Closure                                        | web
    |
|        | GET|HEAD | api/user |        | Closure                                        | api,auth:
api |
|        | POST     | signup   | signup | App\Http\Controllers\UserController@postSignUp | web
    |
+--------+----------+----------+--------+------------------------------------------------+----------
----+

编辑 2016 年 11 月 21 日下午 7:00:值得注意的是,当我使用 Laravel 5.3 时,我不确定 Presenter 使用的是什么,但他使用的是具有 路由的项目.php,我只是尝试使用 web.php 来解决,因为它似乎是我需要遵循的教程最接近的东西。我也有 http://localhost/hiro/public/

的链接

【问题讨论】:

  • 你能运行php artisan route:list
  • 这可能是你想要的:|发布 |注册 |注册 |应用\Http\Controllers\UserController@postSignUp |网络
  • 该错误,因为您正在 GET 方法中访问该路由,但在您的路由代码中,您只允许方法 POST Route::post('/signup', 允许该 URL 中的其他方法 Route::match('GET', 'POST', 'PUT', etc...)
  • @NewbeeDev 我不这么认为。这将导致 MethodNotAllowedHttpException 而不是 NotFoundHttpException。
  • @NewbeeDev 对不起,您在我的代码中哪里看到了 GET?我打算只使用 POST 方法来解决这个问题。除了返回视图('welcome')

标签: php laravel laravel-routing


【解决方案1】:

这是你的服务器配置。

请在您的虚拟主机中使用/hiro/public 作为您的文档根目录。

或者将其添加到您的 .htaccess 文件中:

RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]

【讨论】:

  • 你的意思是像创建一个新的 apache vhost?
  • 不一定。只需编辑现有虚拟主机的DocumentRoot。或者,如前所述,您可以编辑 .htaccess 文件。目标是去掉 URL 中的 /public。
  • 是的,这是为我做的,我几乎想诅咒自己,把头撞在我的桌子、屏幕和笔记本电脑上。我创建了一个名为 hiro 的虚拟主机,并通过 hiro 访问了该应用程序。感谢所有试图帮助我的人。
【解决方案2】:

看来路线本身并没有什么问题。

这对我来说真的只是一个猜测,但值得一试:

web.php

<?php 

// Removed a forward slash from the route
Route::post('signup', [
    'uses' => 'UserController@postSignUp',
    'as' => 'signup'
]);

welcome.blade.php

        <div class="col-md-6">
            <h3>Sign Up</h3>
            <!-- Use a regular HTML route instead of Laravel's own -->
            <form action="/signup" method="POST">
                <div class="form-group">
                    <label for="email">Your Email</label>
                    <input type="form-control" type="text" name="email" id="email">
                </div>
                <div class="form-group">
                    <label for="first_name">Your First Name</label>
                    <input type="form-control" type="text" name="first_name" id="first_name">
                </div>
                <div class="form-group">
                    <label for="password">Your Password</label>
                    <input type="form-control" type="password" name="password" id="password">
                </div>
                <!-- Changed the submit button to input element instead of button -->
                <input type="submit" class="btn btn-primary" value="Submit">
                <input type="hidden" name="_token" value="{{Session::token()}}">
            </form>
        </div>

【讨论】:

  • 不同的错误。找不到对象!在此服务器上找不到请求的 URL。引用页面上的链接似乎是错误的或过时的。请将该错误通知该页面的作者。如果您认为这是服务器错误,请联系网站管理员。
  • 如果您出于测试目的尝试将路由更改为GET 并尝试返回一些东西会发生什么? F.e:Route::get('/signup', function () { return "Hello World!"; });。它会返回“Hello World!”吗?
  • @AkiraHora 你能发布你的服务器配置(虚拟主机)吗?我认为这不是 laravel 的问题。
猜你喜欢
  • 2016-06-17
  • 1970-01-01
  • 1970-01-01
  • 2017-07-24
  • 2014-11-07
  • 2016-12-02
  • 1970-01-01
  • 1970-01-01
  • 2017-10-10
相关资源
最近更新 更多