简体   繁体   English

如何在laravel中获取url参数?

[英]How can i get url parameter in laravel?

This is my url -> http://localhost:82/?search=asd这是我的网址 -> http://localhost:82/?search=asd

How do I catch 'asd' in route?我如何在路线中捕捉“asd”?

I try this -> Route::get('/?search={SearchValue}', 'TryController@search');我试试这个 -> Route::get('/?search={SearchValue}', 'TryController@search');

But it didn't work.但它没有用。 It won't even get into the controller.它甚至不会进入控制器。

You don't have to adjust the routes.您不必调整路线。 But include $request in your controller method.但是在您的控制器方法中包含 $request 。 Then use your request object to access it.然后使用您的请求对象来访问它。

use Illuminate\Http\Request;

public function search(Request $request) {
    // to access the query parameters
    $search = $request->query->get('search');

    // similar but different syntax
   $search = $request->query('search');

    // generic method that checks all input including query
    $search = $request->input('search');
}

You should just only need something like this:你应该只需要这样的东西:

Route::get('/search', 'TryController@search')->name('try.search');

Once you have the route correctly set you can call:正确设置路线后,您可以调用:

public function search(Request $request)
{
   $request->get('search')

to get the url parameter you passed into the request.获取您传递到请求中的 url 参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM