php - 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 的新手。谢谢。
解决方案
在 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'));
}
}
推荐阅读
- java - 给定驱动器的工作目录是什么?
- c - “函数调用中的参数太少”(Visual Studio)。我无法打开文件。如何解决这个问题?
- dialogflow-cx - Dialogflow CX 中表单填写的可选参数
- sql - SQL 联接、分组依据、日期
- c# - 如何让我的分数计时器在死亡时停止?
- docker - 使用 GitHub Actions 构建 Docker 映像:没有这样的文件或目录
- tensorflow-federated - 集中式与联合式收敛
- json - 如何获取 MongoDB webhook 授权 json 的 URL 参数?
- sql - 使用 Powershell 创建具有 ODBC 连接的数据集
- javascript - 如何使用 Selling Partner API 通过 Amazon Renewed 计划更新产品?