【发布时间】:2019-09-12 17:42:14
【问题描述】:
我有 Laravel 5.3 网站。
我今天添加了一些api路由,但它们没有被调用。
确实,我在Http/Api 下有一个NewRouteController 文件
但是路由返回 404 错误。永远无法到达控制器。
api.php 中还有其他可以正常工作的路由(例如,调用其中一个路由api/workingroute)。但是如果我在api.php 中注释掉他们的路线,他们仍然可以工作!
这里是 api.php 文件:
Route::group(['prefix' => 'api'], function () { Route::resource('workingroute', 'Api\WorkingRouteController'); });
Route::group(['prefix' => 'api'], function () { Route::resource('newroute', 'Api\NewRouteController'); });
我认为问题是route:list揭示的
GET|HEAD | api/api/newitems | newitems.index | App\Http\Controllers\Api\New RouteController@index | api |
| | POST | api/api/newitems | newitems.store | App\Http\Controllers\Api\New Route@store | api |
| | GET|HEAD | api/api/newitems/create | newitems.create | App\Http\Controllers\Api\New Route@create | api |
| | GET|HEAD | api/api/newitems/{newitem} | newitems.show | App\Http\Controllers\Api\New Route@show | api |
| | DELETE | api/api/newitems/{newitem} | newitems.destroy | App\Http\Controllers\Api\New Route@destroy | api |
| | PUT|PATCH | api/api/newitems/{newitem} | newitems.update | App\Http\Controllers\Api\New Route@update | api |
| | GET|HEAD | api/api/newitems/{newitem}/edit | newitems.edit | App\Http\Controllers\Api\New Route@edit | api
不知道为什么我们有 api/api,因为我看不到任何地方我会以这种方式指示路线,特别是因为我只是重复所有存在于工作 api 路线的东西。或者我想因为我使用的是 api.php,所以前缀已经添加了。
另外,奇怪的是,对于我的工作路线,我有一堆 api/route 形式的条目,但它们也有 api/api/route 形式的重复条目
对于 api/api/item 形式的路由,我们有这些行
api/api/item/{item} | item.show | App\Http\Controllers\Api\ItemController@show | api
对于 api/item 形式的路由,这些行:
GET|HEAD | api/item/{item} | lesson.show | App\Http\Controllers\Api\ItemController@show | web,auth,admin
我查看了仍然在我的文件夹结构中的 Laravel 5.2 保留的 routes.php,我们有这些行:
// Api
Route::group(['middleware' => ['api', 'auth', 'admin']], function () {
Route::group(array('prefix' => 'api'), function() {
RegisterResourceRoute('item', 'Item');
我想也许是我从 5.2 升级到 5.3 时弄坏了一些东西。奇怪的是,这个 routes.php 定义的路由是 api/item 形式的路由,它们可以工作。现在的问题是如何使路由恢复正常,以便所有 api 路由的形式为 api/item 并且都具有web,auth,admin?
另外,对于它的价值(不确定缓存是否会以某种方式影响我的问题),在 bootstrap/cache 文件夹以及所有存储文件夹中有 .gitignore。
这里是cache.php:
return [
/*
|--------------------------------------------------------------------------
| Default Cache Store
|--------------------------------------------------------------------------
|
| This option controls the default cache connection that gets used while
| using this caching library. This connection is used when another is
| not explicitly specified when executing a given caching function.
|
*/
'default' => env('CACHE_DRIVER', 'file'),
/*
|--------------------------------------------------------------------------
| Cache Stores
|--------------------------------------------------------------------------
|
| Here you may define all of the cache "stores" for your application as
| well as their drivers. You may even define multiple stores for the
| same cache driver to group types of items stored in your caches.
|
*/
'stores' => [
'apc' => [
'driver' => 'apc',
],
'array' => [
'driver' => 'array',
],
'database' => [
'driver' => 'database',
'table' => 'cache',
'connection' => null,
],
'file' => [
'driver' => 'file',
'path' => storage_path('framework/cache'),
],
'memcached' => [
'driver' => 'memcached',
'servers' => [
[
'host' => env('MEMCACHED_HOST', '127.0.0.1'),
'port' => env('MEMCACHED_PORT', 11211),
'weight' => 100,
],
],
],
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
],
/*
|--------------------------------------------------------------------------
| Cache Key Prefix
|--------------------------------------------------------------------------
|
| When utilizing a RAM based store such as APC or Memcached, there might
| be other applications utilizing the same cache. So, we'll specify a
| value to get prefixed to all our keys so we can avoid collisions.
|
*/
'prefix' => 'laravel',
];
想法?
谢谢, 布赖恩
【问题讨论】:
-
使用
php artisan route:list查看注册的路由和url路径是什么。里面没有显示吗?尝试运行php artisan optimize并重新运行路由列表命令,看看它是否出现。 -
路由列表的问题是它使用 PHP CLI,在我的 MAC 上,CLI 是 PHP 5.6,并且 route:list 正在生成语法错误。将升级 CLI,但试图解决问题的核心。我只是通过编辑 api.php 和创建控制器来创建路由,而不是使用工匠命令,所以这可能会改变 Laravel 处理此类手动创建的路由的方式。也许存储文件夹中有东西,但那里没有 routes.php。
-
您可以尝试手动清除或重命名/移动
bootstrap/cache文件夹(如果存在) -
你能把整个
api.php路由文件贴出来吗? -
嗨,是的,api 路由文件只有帖子中显示的那种行。我编辑并添加了控制器文件。问候,布赖恩
标签: laravel