2016-07-27 98 views
0

我正在通過自我鍛鍊來學習Laravel。基本上我想實現的是參數'id'可以從路由傳遞給控制器​​,以便在單擊鏈接時獲取該特定ID的不同主題列表。我檢查了所有其他帖子,似乎我無法在那裏得到答案。如何處理Laravel NotFoundHttpException?

這是我在Route.php中的代碼,其中mainpage是我的應用程序的索引頁。

Route::get('/public/', array('as' => 'Personal_assistant' , 'uses' => '[email protected]_mainpage')); 
Route::get('public/{id}' , array('as' => 'viewtopic' , 'uses' => '[email protected]_topic')); 

我在控制器 'category_by_user' 代碼:

class Category_by_user extends Controller 
{ 
// 

public $restful = true; 

public function get_mainpage() 
{ 

$data = DB::table('categories') 
     -> select('category_name', 'category_description') 
    //->where('category_user_id', '=' , $user_id) 
    ->get(); 

    return view::make('mainpage') 
     ->with('title', 'Category By User') 
     ->with('data' , $data); 
    } 


    public function get_topic($id) 
    { 
     $data1 = DB::table('topics') 
     ->where('Category_id', '=' , $id) 
     ->get(); 


     return View::make('viewtopic') 
     ->with('title', 'All Topic by Category') 
     ->with('data1' , $data1) 
     ->with('id' , $id); 
     } 
} 

我在viewtopic.blade.php代碼:

{h1>Personal Info Assistant</h1> 

    <!-- will be used to show any messages --> 
    @if (Session::has('message')) 
     <div class="alert alert-info">{{ Session::get('message') }}</div> 
    @endif 

    <table class="table table-striped table-bordered"> 

    <tbody> 
    <!-- // List all categories belongs to that particular user --> 


    @foreach($data1 as $key => $topic) 
     <tr> 

     <td>{{ $topic->topic_description }}</td> 
     <td>ID = {{ $id }}</td> 

     </tr> 
    @endforeach 
    </tbody> 
</table> 

</div> 
</body> 
</html> 

錯誤日誌細節如下:

in RouteCollection.php line 161 
    at RouteCollection->match(object(Request)) in Router.php line 821 
    at Router->findRoute(object(Request)) in Router.php line 691 
    at Router->dispatchToRoute(object(Request)) in Router.php line 675 
    at Router->dispatch(object(Request)) in Kernel.php line 246 
    at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) 
    at call_user_func(object(Closure), object(Request)) in Pipeline.php  line 52 
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Debugbar.php line 49 
    at Debugbar->handle(object(Request), object(Closure)) 
    at call_user_func_array(array(object(Debugbar), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) 
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44 
    at CheckForMaintenanceMode->handle(object(Request), object(Closure)) 
    at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) 
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) 
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103 
    at Pipeline->then(object(Closure)) in Kernel.php line 132 
    at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99 
    at Kernel->handle(object(Request)) in index.php line 54 
    at require_once('C:\wamp\www\Personal_Info_App\public\index.php') in server.php line 21 

非常感謝您的支持任何幫助。

+0

到目前爲止,我可以看到你定義的路線有問題。嘗試(可以肯定)'/ public'而不是'/ public /'和'/ public/{id}'而不是'public/{id}'。 –

回答

0

你可以顯示鏈接和通過鏈接的代碼ID嗎?也許$ id在你的href attr中沒有正確傳遞。

+0

我不認爲'id'是問題,因爲它是'NotFoundHttpException'。但是,如果我們看到他所要求的網址,這將非常有幫助。 –

+0

![主頁](http://localhost/mainpage.jpg)。 –

+0

![viewtopic](http://localhost/viewtopic.jpg)。 –