首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Laravel动态路由

Laravel动态路由
EN

Stack Overflow用户
提问于 2015-04-06 18:32:35
回答 1查看 3.6K关注 0票数 2

我在做一个电子商务应用程序。我需要动态产品/类别url根据他们的url_key (一个列的url)。

我想做这样的事:

代码语言:javascript
运行
复制
    if (Route::has('route.name')) {
        // go where ever it is intended
    } 
    else {

        $productHelper = new ProductHelper();

        $product = $productHelper->getProductByUrl($url_key);
        if ($product)
            return Redirect::to('product')->with('id', $product->id);
        else {
            $categoryHelper = new CategoryHelper();

            $category = $categoryHelper->getCategoryByUrl($url_key);

            if ($category)
                return Redirect::route('category')->with('id', $category->id);
            else
                return View::make('static.page_not_found');
        }
    }

让我们说我们有这样的网址:

/关于-我们/联系-我们

/sony-bravia-b32 (产品的url键) /tv-led (类别的url键)

现在,我想检查一下,如果"routes.php“文件中存在一个url,那么它应该转到那个url (包含类似于表单提交的数据)。

否则,它将检查url是否与产品或类别的url键相匹配,并相应地移到那里。

马根托的行为。我被困在if部分了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-06 18:55:05

您可以通过使用routes.php来做到这一点。

这就是你怎么做的例子。在实际应用程序中,您可能需要为每个路由创建单独的控制器,以处理应用程序的不同行为。

Routes.php

代码语言:javascript
运行
复制
Route::get('/about-us', function(){
  echo 'About Us';
});

Route::get('/contact-us', function(){
  echo 'Contact Us';
});


Route::get('/{product}/{category}', function($product, $category){
  dd($product,$category); // remove this line and put your business logic here
});

确保将静态url置于动态url之上。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29477292

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档