【问题标题】:Cake PHP 3 Paring Routing with a tableCake PHP 3 Paring Routing with a table
【发布时间】:2017-11-10 20:16:18
【问题描述】:

我一直在尝试使用带有 Cake 的路由器获取漂亮的 url。

www.mysite.com/category/slug

www.mysite.com/slug

www.mysite.com/category/

我认为没有公式可以准确地得到我想要的,所以我想明确匹配每个 url。我希望我的路由表看起来像这样

  id | url | controller | action | param 

所以当有人去 www.mysite.com/mysubcategory/my-title-of-page

我希望它与 url 列交叉引用“mysubcategory/my-title-of-page”并继续。最好的方法是什么?

我的猜测是将所有流量引导到路由控制器

Router::connect('/*', array('controller' => 'Routes', 'action' => 'redirs'));

但是当它重定向时,我希望它在浏览器中显示漂亮的 url,我不希望它显示articles/view/12

谢谢

【问题讨论】:

    标签: php cakephp cakephp-3.x


    【解决方案1】:

    您可以使用从数据库中检索到的信息动态创建路由,或者使用查找表的自定义路由类。如果您只有几条路线,那么您可能想要使用前者。

    在您的routes.php 中,只需阅读您的表格并连接路线,它应该就这么简单。这是一个非常基本的例子:

    use Cake\ORM\TableRegistry;
    
    $Routes = TableRegistry::get('Routes');
    $routes = $Routes->find();
    
    foreach ($routes as $route) {
        Router::connect(
            $route->url,
            [
                'controller' => $route->controller,
                'action' => $route->action
            ],
            [
                'pass' => [$route->param]
            ]
        );
    }
    

    对于动态的东西,例如检查 Mapping slugs from database in routing

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 2021-03-31
      • 2019-03-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 1970-01-01
      相关资源
      最近更新 更多