Laravel 执行显式绑定会给我带来一个错误
我正在尝试在Providers/RouteServiceProvider.php中进行显式绑定。当我按照文档中所写的方式进行操作时,我会发现一个错误,显示了一个不正确的模型路径。但是添加。。在此之前,我的路径不起作用,因为它不是字符串,只添加了一个错误Laravel 执行显式绑定会给我带来一个错误,laravel,Laravel,我正在尝试在Providers/RouteServiceProvider.php中进行显式绑定。当我按照文档中所写的方式进行操作时,我会发现一个错误,显示了一个不正确的模型路径。但是添加。。在此之前,我的路径不起作用,因为它不是字符串,只添加了一个错误 public function boot() { // parent::boot(); Route::model('post', App\Post::class); }
public function boot()
{
//
parent::boot();
Route::model('post', App\Post::class);
}
我得到一个错误,模型路径不正确:
Class App\Providers\App\Post does not exist
它尝试在providers文件夹中查找路径。。
但文档示例正好显示了这一点,文档示例:
public function boot()
{
parent::boot();
Route::model('user', App\User::class);
}
我怀疑这是因为它位于名称空间App\Providers中,但不知道该怎么办。多谢各位
此处为整个服务提供商类:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Route;
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.
*
* @return void
*/
public function boot()
{
//
parent::boot();
Route::model('post', \App\Post::class);
}
/**
* 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'));
}
}
带有\
的简短解决方案前缀,以便命名空间从根开始,而不是从当前根开始
Route::model('post',\App\post::class)代码>
其他解决方案导入顶部的类:
<?php
use App\Post;
...
public function boot()
{
//
parent::boot();
Route::model('post', Post::class);
}
...
那么\App\Post::class
呢?@Chay22它确实存在于App
文件夹中,并且具有命名空间App
我尝试了两种方法:导入顶部的模型和添加“\”,但它给我带来了一个错误Class-App\Http\Controllers\App\Post不存在
请将整个文件的代码添加到question@MantasValuckas这意味着提供程序中的错误消失了。现在你必须修复控制器中的语法导入。事实上,你现在需要更新控制器,使用\App\Post
而不是App\Post
@SimonDepelchin我用什么方式更新我的控制器,对不起,如果我问的问题看起来很愚蠢,我对这个framefork很陌生。我在我的提供者中添加了\App\Post
<?php
use App\Post;
...
public function boot()
{
//
parent::boot();
Route::model('post', Post::class);
}
...