【发布时间】:2017-12-13 01:10:23
【问题描述】:
当我删除初始 use Illuminate\Http\Request 并改为在 Controller 文件中添加 use App\Item 时,items/create 路由以 404 响应。我怎样才能仍然使用 App\Item 命名空间并到达 items/create 路由?我已经尝试添加两者,但不起作用。
web.php
Route::get('items', 'ItemsController@index');
Route::get('items/{item}', 'ItemsController@show');
Route::get('items/create', 'ItemsController@create');
ItemsController.php
<?php
namespace App\Http\Controllers;
use App\Item;
class ItemsController extends Controller
{
public function index(){
$items = Item::all();
return view('items.index', ['items' => $items]);
}
public function show(Item $item){
return $item->body;
}
public function create(){
return view('items.create');
}
}
Item.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Item extends Model
{
//
}
【问题讨论】:
-
你能提供Item模型的代码吗?
-
它只是一个标准模型,还没有任何内容。不过,我已经添加了它。
-
所以经过多一点测试,我发现Controller文件中的命名空间可能不是问题,但可能在web.php文件中。我已经删除了除 items/create 之外的所有路由和关联的控制器功能,它会转到页面。