【问题标题】:Laravel 5.2.31: routing: 404 not found error messageLaravel 5.2.31:路由:404 未找到错误消息
【发布时间】:2016-09-11 10:05:42
【问题描述】:

使用 Laravel 框架版本 5.2.31 和 Apache/2.4.12 (Ubuntu) 服务器。收到以下错误消息:

404 Not Found 在此服务器上未找到请求的 URL /laravel/public/signup。

感谢您的帮助。

下面是 routes.php(该文件不再准确。请参阅下面的编辑部分中的新文件)

<?php

Route::group(['middleware' => ['web']], function() 
{
    Route::get('/', function () 
    {
        return view('welcome');
    });
    Route::post('/signup', [
        'uses' => 'UserController@postSignUp',
        'as' => 'signup'
    ]);
});

下面是 UserController.php 文件

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;

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

        $user = new User();
        $user->email = $email;
        $user->prenom = $prenom;
        $user->motDePasse = $motDePasse;

        $user->save();

        return redirect()->back();
    }

    public function postSignIn(Request $Request)
    {

    }
}

编辑

下面是routes.php

<?php

Route::get('/', function () 
{
    return view('welcome');
});
Route::post('/signup', [
    'uses' => 'UserController@postSignUp',
    'as' => 'signup'
]);

下面是RouteServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    public function boot(Router $router)
    {
        //

        parent::boot($router);
    }

    /**
     * Define the routes for the application.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    public function map(Router $router)
    {
        $this->mapWebRoutes($router);

        //
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    protected function mapWebRoutes(Router $router)
    {
        $router->group([
            'namespace' => $this->namespace, 'middleware' => 'web',
        ], function ($router) {
            require app_path('Http/routes.php');
        });
    }
}

【问题讨论】:

    标签: laravel


    【解决方案1】:

    将此添加到 .htaccess

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

    并更改路径:

    打开本地/bootstrap/paths.php

    'public' => __DIR__.'/../public', to 'public' => __DIR__.'/../../',
    

    打开 local/bootstrap/paths.php 并找到以下代码

    'public' => DIR.'/../public', 用下面的代码改变

    'public' => DIR.'/../../',

    Open index.php (on root) and find below code
    
    require __DIR__.'/../bootstrap/autoload.php';
    $app = require_once __DIR__.'/../bootstrap/start.php';
    

    用下面的代码改变

    require __DIR__.'/local/bootstrap/autoload.php';
    $app = require_once __DIR__.'/local/bootstrap/start.php';
    

    现在你已经设置好了你可以访问 url http://example.com/ 如果仍然有问题尝试清除浏览器缓存。

    【讨论】:

    • 我使用的是 Apache/2.4.12。该行已在 /etc/apache2/mods-enabled 中取消注释为 LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
    【解决方案2】:

    首先,您似乎在routes.php 中使用post 方法,但您尝试使用get 方法加载路由。

    其次,您的网络服务器配置错误。您的 Laravel 项目的 Web 服务器 should be pointed to a public directory(您应该为其创建 VirtualHost 并重新启动 Web 服务器)。

    另外,从routes.php 中删除web 中间件,因为it will cause errors。

    【讨论】:

    • 我已经根据您提供的链接编辑了文件route.php。我在我的服务器上检查了文件RouteServiceProvider.php;它的内容和这里的一样[链接]github.com/laravel/laravel/commit/…错误信息404 page not found仍然显示。
    • 你在我的回答中做了另外两件事吗?
    猜你喜欢
    • 2015-02-04
    • 2019-05-20
    • 2017-06-17
    • 2020-10-26
    • 2014-06-25
    • 2016-10-20
    • 2014-10-02
    • 2013-10-07
    • 1970-01-01
    相关资源
    最近更新 更多