Reputation: 15
I'm currently studying laravel and for my small project, and I'm having a small issues now.
I'm trying to handle php input in url i.e http://example.com/?page=post&id=1
I currently have this in my controller for 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);
}
and this is the controller
Route::get('/', 'TestController@index');
Route::get('/{query}', 'TestController@post' );
How to process the php input to be routed to controller? I'm very confused right now, I've tried several other method for the Route::get
Upvotes: 1
Views: 41
Reputation: 1212
Why do you need to use query parameters in url. You can simply use this structure http://example.com/posts/1
Then your routes will look like this:
Route::get('/posts/{post}', 'PostsController@show');
And you will be able to access Post
model instantly in your show method.
Example:
public function show(Post $post) {
return view('html.post', compact('post'));
}
Look how small is your code now.
Upvotes: 0
Reputation: 163948
This route Route::get('/', 'TestController@index')
directs user to the index
route. So, if you can't change URL structure and you must use this structure, you should get URL parameters in the index
route like this:
public function index()
{
$page = request('page');
$id = request('id');
Upvotes: 1
Reputation: 953
You can use it a as parameter on your controller :-) see this answer please: https://laravel.io/forum/07-26-2014-routing-passing-parameters-to-controller
for example query parameter in route would be $query parameter in controller method :-)
So like this:
Route::get('/{query}', 'TestController@post' );
//controller function
public function controllerfunc($query){}
Upvotes: 0