2017-02-18 83 views
2

首先,我是一個總的Laravel noob,但我想學習它。我從教程https://laravel.com/docs/5.2/quickstart開始,但安裝了5.4。這就是它出錯的地方,因爲路線的位置與Laravel 5.2版相比有所不同,本教程基於此。因此,在我的根文件夾,我有/路由加入在/routes/web.php教程代碼:在基本的Laravel 5.4教程中找不到類「任務」

<?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('/'); 
}); 

?> 

我做了一個應用程序/ Task.php,其中包含了(空)任務類和我數據庫設置正確,據我所知。

FatalErrorException in web.php line 21: 
Class 'Task' not found 

不過,我得到了上面的錯誤,提示我的命名空間有問題,但我似乎無法正確理解。

順便說一句,爲了讓安裝正常工作,我將根文件夾中的server.php重命名爲index.php,並將.htaccess從/ public複製到我的根文件夾中。

任何幫助,將不勝感激!

+1

使用'$任務= \軟件\任務::排序依據( 'created_at', 'ASC') - >獲得();'。 –

回答

5

使用正確的命名空間

/** 
    * Show Task Dashboard 
    */ 

    Route::get('/', function() { 
     $tasks = \App\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 \App\Task; 
     $task->name = $request->name; 
     $task->save(); 

     return redirect('/'); 
    }); 

    /** 
    * Delete Task 
    */ 
    Route::delete('/task/{task}', function (\App\Task $task) { 
     $task->delete(); 

     return redirect('/'); 
    }); 
+0

謝謝你。你知道在拉拉維爾發生了什麼特別的變化,導致了這種情況的發生嗎? – limco