【发布时间】:2017-07-08 05:56:24
【问题描述】:
首先,我完全是 Laravel 菜鸟,但我想学习它。我从https://laravel.com/docs/5.2/quickstart 的教程开始,但安装了 5.4。这就是问题所在,因为与本教程所基于的 Laravel 5.2 版本相比,路线的位置不同。所以在我的根文件夹中,我有 /routes 并在 /routes/web.php 中添加了教程代码:
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
/**
* Show Task Dashboard
*/
Route::get('/', function () {
$tasks = Task::orderBy('created_at', 'asc')->get();
return view('tasks', [
'tasks' => $tasks
]);
});
/**
* Add New Task
*/
Route::post('/task', function (Request $request) {
$validator = Validator::make($request->all(), [
'name' => 'required|max:255',
]);
if ($validator->fails()) {
return redirect('/')
->withInput()
->withErrors($validator);
}
$task = new Task;
$task->name = $request->name;
$task->save();
return redirect('/');
});
/**
* Delete Task
*/
Route::delete('/task/{task}', function (Task $task) {
$task->delete();
return redirect('/');
});
?>
我已经创建了一个 app/Task.php,其中包含(空)Task 类,据我所知,我的数据库设置正确。
FatalErrorException in web.php line 21:
Class 'Task' not found
我仍然收到上述错误,表明我的命名空间有问题,但我似乎无法正确处理。
顺便说一句,为了使安装正常工作,我已将根文件夹中的 server.php 重命名为 index.php 并将 .htaccess 从 /public 复制到我的根文件夹。
任何帮助将不胜感激!
【问题讨论】:
-
使用
$tasks = \App\Task::orderBy('created_at', 'asc')->get();。
标签: php .htaccess laravel routes namespaces