首页 > 解决方案 > Laravel 找不到路径 404

问题描述

所以我的路线:

Route::match(array('GET', 'POST'),'object/create', 'ObjectController@create');

和 ObjectController 处理程序 ->

class ObjectController extends Controller
{

public function create(Request $request){

        $fieldNames = array(
           .
           .
           .
        );

        $validator = Validator::make($request->all(), $rules);
        $validator->setAttributeNames($fieldNames);

        if ($validator->fails()) 
        {
            return back()->withErrors($validator)->withInput();
        }
        else
        {
         .
         .
         .

    }

当我尝试访问 www.xxx.com/object/create 时,它​​会给出一个 404 找不到请任何想法?我是 laravel 的新手。谢谢。

标签: phplaravelrouteshttp-status-codes

解决方案


在 Laravel 中,只有在 Routes/web.php 中定义了路由,www.xxx.com/object/create 才会起作用,您也可以使用 route-group Route::group(['namespace' => 'Api'], function () { 检查你的路由和控制器的命名空间是否相同,并检查你的路由是否有前缀。如果以上都正确,检查 app/provider/RouteServiceProvider.php

它应该是这样的:

命名空间应用\提供者;

使用 Illuminate\Support\Facades\Route;使用 Illuminate\Foundation\Support\Providers\RouteServiceProvider 作为 ServiceProvider;

class RouteServiceProvider extends ServiceProvider { /** * 此命名空间应用于您的控制器路由。* * 另外,它被设置为 URL 生成器的根命名空间。* * @var string */ protected $namespace = 'App\Http\Controllers';

/**
 * Define your route model bindings, pattern filters, etc.
 *
 * @return void
 */
public function boot()
{
    //

    parent::boot();
}

/**
 * Define the routes for the application.
 *
 * @return void
 */
public function map()
{
    $this->mapApiRoutes();

    $this->mapWebRoutes();

    //
}

/**
 * Define the "web" routes for the application.
 *
 * These routes all receive session state, CSRF protection, etc.
 *
 * @return void
 */
protected function mapWebRoutes()
{
    Route::middleware('web')
         ->namespace($this->namespace)
         ->group(base_path('routes/web.php'));
}

/**
 * Define the "api" routes for the application.
 *
 * These routes are typically stateless.
 *
 * @return void
 */
protected function mapApiRoutes()
{
    Route::prefix('api')
         ->middleware('api')
         ->namespace($this->namespace)
         ->group(base_path('routes/api.php'));
}

}


推荐阅读