2017-01-27 107 views
1

我目前正在學習laravel和我的小項目,現在我遇到了一些小問題。路由到控制器問題

我試圖來處理URL即PHP輸入http://example.com/?page=post&id=1
我現在有這在我的控制器post.blade.php

public function post($Request $request) 
{ 
    $page = $request->input('page'); 
    $id_rilisan = $request->input('id'); 
    $post = Rilisan::where('id_rilisan', '=', $id_rilisan)->first(); 
    if($post = null) 
    { 
    return view('errors.404'); 
    } 
    return view('html.post') 
      ->with('post', $post); 
} 

,這是控制器

Route::get('/', '[email protected]'); 
Route::get('/{query}', '[email protected]'); 

如何處理php輸入路由到控制器?我現在很迷茫,我已經試過了路線等幾個方法::獲得

回答

1

這條路線Route::get('/', '[email protected]')引導用戶到index路線。所以,如果你不能改變URL結構,你必須使用這個結構,你應該得到的URL參數,在這樣的index路線:

public function index() 
{ 
    $page = request('page'); 
    $id = request('id'); 
+0

所以我不需要其他Route :: get? – nothingexceptme

+0

@nothingexceptme如果你不使用它,你可以刪除它。第二條路由將會捕獲像'/ some-string'這樣的URIs –

0

爲什麼你需要在URL中使用查詢參數。你可以簡單地用這個結構http://example.com/posts/1

然後你的路線將如下所示:

Route::get('/posts/{post}', '[email protected]'); 

而且你將能夠即時訪問Post模型在展示方法。 示例:

public function show(Post $post) { 
    return view('html.post', compact('post')); 
} 

看看您的代碼現在有多小。

+0

感謝您的建議,但我希望它是這樣的,並且函數應該從數據庫獲取數據 – nothingexceptme

+0

這也是從數據庫獲取的。 Post模型將從數據庫中獲取所需的一切。 – zgabievi