2017-01-27 109 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