简体   繁体   English

我如何传递参数使用? 在 url 在 Laravel

[英]How can i pass parameter using ? in url in Laravel

I want to pass parameter using?我想传递参数使用? in url, like在 url,喜欢

http://example.com/somthing?name=myname&phone=12121212 http://example.com/somthing?name=myname&phone=12121212

when i add a '?'当我添加一个“?” in Route file it shows 404在路由文件中显示 404

In my route file在我的路线文件中

Route::get('test?id={id}&name={name}','TestController@test')->name('frontend.test');

My controller我的 controller

public function test($id, $name)
{
    
    dd($name);
}

But when i use it like this works fine但是当我像这样使用它时效果很好

Route File:路由文件:

Route::get('test/id={id}&name={name}','TestController@test')->name('frontend.test');

Controller: Controller:

public function test(Request $request)
{
    
    dd($request->route()->parameters());
}

i have a project with raw php i want to convert it to Laravel, so i don't want to change the url structure.我有一个原始 php 项目,我想将其转换为 Laravel,所以我不想更改 url 结构。

how can i achieve this in Laravel route.我怎样才能在 Laravel 路线中实现这一目标。

I wouldn't define the parameters within the route.我不会在路线中定义参数。

Route::get('test','TestController@test')->name('frontend.test');

I prefer to validate and get the request parameters like this:我更喜欢像这样验证并获取请求参数:

use Illuminate\Http\Request;

...

public function test(Request $request)
{
    $params = $request->validate([
        'id' => 'required|numeric',
        'name' => 'required|alpha'
    ]);

    dd($params['id'], $params['name']);
}

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

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