Laravel:将输入查询的结果更改为友好url

Laravel:将输入查询的结果更改为友好url,laravel,forms,seo,Laravel,Forms,Seo,如何更改提交表单时生成的此URL- http://localhost:8000/estates?zone=London&type=villa 要访问此URL,请执行以下操作: http://localhost:8000/estates/London/villa 需要使URL对搜索引擎更友好 我从表单中的输入字段中获取区域和别墅 localhost:8000/地产 当我提交表单时,我会得到如下URL- class MyController { public function create(

如何更改提交表单时生成的此URL-

http://localhost:8000/estates?zone=London&type=villa

要访问此URL,请执行以下操作:

http://localhost:8000/estates/London/villa

需要使URL对搜索引擎更友好

我从表单中的输入字段中获取区域别墅

localhost:8000/地产

当我提交表单时,我会得到如下URL-

class MyController
{
    public function create()
    {
        // Your form
    }

    public function store()
    {
        // This is where you receive the zone and villa in the request
    }
}
public function store(Request $request)
{
    // Your code here
    redirect()->to($request->zone.'/'.$request->villa);
}
public function anotherMethod($zone, $villa)
{
    // Access your $zone and $villa here
}
localhost:8000/地产?区域=伦敦&类型=别墅

而不是上面我想有这个网址,当我提交表格-


localhost:8000/estates/London/villa

您应该重新调整路线,使区域和村庄成为路线参数

因此,例如,的路由将是
route::post('/estates/{zone}/{villa}','SomeController@action)
在控制器中,可以将区域和村庄作为参数注入。所以你可以用这样的方法:

class SomeController {
         public function action(Request $request, string $zone, string $villa){

         }
}

在Laravel docs中的Routing的Route parameters(路由参数)部分有进一步的描述。

当您提交表单时,它应该在控制器操作中捕获post数据,如下所示-

class MyController
{
    public function create()
    {
        // Your form
    }

    public function store()
    {
        // This is where you receive the zone and villa in the request
    }
}
public function store(Request $request)
{
    // Your code here
    redirect()->to($request->zone.'/'.$request->villa);
}
public function anotherMethod($zone, $villa)
{
    // Access your $zone and $villa here
}
正如您在store方法中收到请求中的输入字段一样,现在可以执行类似的操作-

class MyController
{
    public function create()
    {
        // Your form
    }

    public function store()
    {
        // This is where you receive the zone and villa in the request
    }
}
public function store(Request $request)
{
    // Your code here
    redirect()->to($request->zone.'/'.$request->villa);
}
public function anotherMethod($zone, $villa)
{
    // Access your $zone and $villa here
}
请确保已为区域和别墅创建了路由,否则重定向到不存在的路由/url将无法工作

为您的请求创建这样的路由-

Route::get('estates/{zone}/{villa}', 'MyController@anotherMethod');
您将在控制器中使用另一种操作方法来接收此区域别墅这样的输入-

class MyController
{
    public function create()
    {
        // Your form
    }

    public function store()
    {
        // This is where you receive the zone and villa in the request
    }
}
public function store(Request $request)
{
    // Your code here
    redirect()->to($request->zone.'/'.$request->villa);
}
public function anotherMethod($zone, $villa)
{
    // Access your $zone and $villa here
}

dud我忘了写这句话:我从中输入了zone和villa,想在他们提交表单时将此url更改为此url,我将更改q,sry我的baddud还有一个q,它在搜索引擎中运行良好吗?这个问题是为我不好在这种情况下!参数URL不利于SEO。因此,是的,像这样的房地产/区域/别墅的URL结构更好。你可以在这里了解更多关于这件事的信息-