Laravel路由变化 [英] Laravel routing varying

查看:106
本文介绍了Laravel路由变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类别表,如下所示:

I have a categories table that looks like this:

-- id   -- parent_id             -- name
    1             0              electronics
    2             1              Televisions
    3             2             LCD TVs
    4             2             Plasma Tvs
    5             1              DVD Players
    6             1              Bluray Players
    7             0             Home Appliances
    8             7            Cookers
    9             7            Fridges & Freezers
    10            0            Fashion
    11            10            Mens Clothing
    12            11             Shirts
    13            10            Womens Clothing
    14            13            Skirts

等等-我遇到的问题是某些类别仅是单个类别,而其他类别可以是3或4深。

and so on - the problem I have is some categories are only single and others can be 3 or 4 deep.

我有一个列表页面,该页面将对给定类别中的所有项目进行分页,并显示该类别的子类别以进行进一步挖掘,依此类推,直到到达最后一个子类别为止。

I have a list page that will paginate all items in a given category and will show the child categories of that category for further drilling down and so on until the last child is reached.

我有2个问题1是如何路由它,以便它与/ products / {parent_category} /?if_exists_child_category / if_exists_child_child / if_exists_child_child_child

I have 2 problems 1 being how do I route this so it will match /products/{parent_category}/?if_exists_child_category/if_exists_child_child/if_exists_child_child_child

下一个是我具有特色产品的主页,需要链接到该产品页面,并包括其完整的类别路径product / parent_cat / if_exists_sub_cat / if_exists_sub_sub_cat / if_exists_sub_sub_sub_ cat / product_name *我有一个想法,路由问题是我的主要问题。

the next is on the home page I have featured products and need to link to the product page and include the full category path to it product/parent_cat/if_exists_sub_cat/if_exists_sub_sub_cat/if_exists_sub_sub_sub_cat/product_name * I have an idea how to do this one the routing issue is my main problem.

或者即使我可以在prodocucts之后捕获所有内容,也可以将其分解成一个数组,循环浏览类别,或者找到产品块并显示正确的视图。

or even if I could just capture everything after prodocucts/ I could then explode it into an array and cycle through the categories and or find the product slug and display the correct view.

在此先谢谢大家,祝圣诞节快乐:)

Thanks All in advance and Happy Almost Christmas :)

推荐答案

我建议您更改URL,以便它们具有用于路由的一部分和用于SEO的一部分。这就是我的意思:

I propose you change your URLs so they have a part that will be used for routing and one that will be used for SEO purposes. Here's what I mean:

/products/view/{product_id}/{category_name}/{category_name}/{etc}/{product_name}
/products/category/{category_id}/{category_name}/{category_name}/{etc}

这样做的好处是您可以忽略以下事实:类别的数量不一定总是相同的。您的路由仅取决于URL的前几个段。

This has the advantage that you can ignore the fact that you won't always have the same numbers of categories. Your routing only depends on the first few segments of the URL.

Route::get('products/view/{id}/{seo-slug?}', array('as' => 'product', function($id, $seoSlug){
    $product = Product::find($id);
    // ...
}))->where('seo-slug', '.*');

Route::get('products/category/{id}/{seo-slug?}', array('as' => 'category', function($id, $seoSlug){
    $category = Category::find($id);
    // ...
}))->where('seo-slug', '.*');

现在,当涉及到生成链接时,仅使用Laravels帮助器功能将很困难。让我们创建自己的。一种方法是向产品和类别模型添加功能。 (您也可以编写独立于模型的函数,核心功能保持不变)

Now when it comes to generating links you will have a hard time just using Laravels helper functions. Let's create our own. One way is to add a function to the Product and Category models. (You could also write a function that's independent of the models, the core functionality stays the same)

public function getUrl(){
    $categoryNames = array('cat1', 'cat2', 'cat3'); // use/create a function on the category model that uses recursion to determine all the categories of the tree
    $seoSlug = implode('/', $categoryNames);
    // ... maybe add other stuff to $seoSlug
    return route('product', array($this->id, $seoSlug));
}



类别



Category

public function getUrl(){
    $categoryNames = array('cat1', 'cat2', 'cat3'); // use the same function as above
    $seoSlug = implode('/', $categoryNames);
    // ... maybe add other stuff to $seoSlug
    return route('category', array($this->id, $seoSlug));
}



重定向错误的 URL



如果需要,您现在可以重定向带有错误SEO段的URL。意味着也可以只输入:

Redirect "wrong" URLs

If you want, you can now redirect URLs that have a wrong SEO slug. Meaning it would also be possible to just enter:

/products/view/183

这也可以用作某种URL缩短,用于在电子邮件等中发送链接。

This can also serve as some kind of URL shortening for sending links in emails etc.

要验证URL,然后重定向,您可以将当前URL与新生成的URL进行比较。像这样:

To validate the URL and then redirect you can just compare the current URL with a "freshly" generated one. Like this:

Route::get('products/view/{id}/{seo-slug?}', array('as' => 'product', function($id, $seoSlug){
    $product = Product::find($id);
    $productUrl = $product->getUrl()
    if(Request::url() !== $productUrl){
        return Redirect::to($productUrl);
    }
    // ...
}))->where('seo-slug', '.*');

哦,也祝您圣诞节快乐!

这篇关于Laravel路由变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆