简体   繁体   English

我想从 laravel 中的 URL 中删除 id

[英]I want to remove id from URL in my laravel

In my Laravel application, I have created url: like "www.example.com/rent/property-for-rent/45" but here, I want to remove id from the last and concatenate id with my 2nd parameter that is, "property for rent" like: "www.example.com/rent/property-for-rent-45".在我的 Laravel 应用程序中,我创建了 url:例如“www.example.com/rent/property-for-rent/45”,但在这里,我想从最后一个中删除 id,并将 id 与我的第二个参数连接起来,即“出租物业”,例如:“www.example.com/rent/property-for-rent-45”。

In the Controller function get 3 parameters在Controller function中得到3个参数

public function single_properties(Request $request, $property_purpose, $slug, $id)

In the Blade file在刀片文件中

{{url(strtolower($property->property_purpose).'/'.$property->property_slug)}}

In the Web.php File在 Web.php 文件中

Route::get('{property_purpose}/{slug}/{id}', 'PropertiesController@single_properties');

I have tried to reduce parameter from the function, blade and route, but didn't work for me.我试图减少 function、刀片和路由的参数,但对我不起作用。 can anyone please help me out, I am poorly trapped in it,谁能帮帮我,我被困在里面了,

you can simply do it like this:你可以简单地这样做:

public function singleProperties(Request $request, $property_purpose, $slug, $id)
{
    // old url: example.com/rent/property-for-rent/45
    // new url: example.com/rent/property-for-rent-45
    $newUrl = $property_purpose . '/' . $slug . '-' . $id;

    return view('your.view.blade', [
        'new_url' => $newUrl,
    ]);
}

And in your blade call {{ $new_url }}在你的刀片调用{{ $new_url }}

You can do in a simple way.你可以用一种简单的方式来做。 As the URL is like this:-由于 URL 是这样的:-

www.example.com/rent/property-for-rent-45

Url structure for route will be like: /rent/slug-id Means id is concatenated with slug.路由的 Url 结构如下: /rent/slug-id 表示 id 与 slug 连接。 so laravel will consider it as full string as slug only.所以 laravel 只会将其视为完整字符串。 We will define this and later in controller will extract id from the slug.我们将定义它,稍后在 controller 将从 slug 中提取 id。

So in routes/ web.php you can define like:-所以在路线/ web.php 你可以定义如下: -

In the Web.php File Route::get('{property_purpose}/{slug}','PropertiesController@single_properties');在 Web.php 文件路径::get('{property_purpose}/{slug}','PropertiesController@single_properties');

In the Blade file在刀片文件中

{{url(strtolower($property->property_purpose).'/'.$property->property_slug-$property->property_id)}}

In Controller File You an define like:-在 Controller 文件中,您可以定义如下:-

Here Slug variable you have to parse using known PHP function.在这里,您必须使用已知的 PHP function 解析 Slug 变量。 As with the information, you know id is coming in last after - symbol.与信息一样,您知道 id 最后出现在 - 符号之后。 So you can explode slug variable and take the id coming last after -因此,您可以分解 slug 变量并在最后获取 id -

public function singleProperties(Request $request, $property_purpose, $slug)
{

$slugarr=explode('-',$slug);
$id=$slugarr[count($slugarr)+1];// As index starts from 0, for getting last you have to this

//Now you got the $id you can fetch record you want this id

}

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

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