【问题标题】:Is it possible to dynamically change view name or create a view that does not exist yet in phalcon?是否可以动态更改视图名称或创建 phalcon 中尚不存在的视图?
【发布时间】:2015-12-25 19:35:35
【问题描述】:

我想知道如何在 phalcon 中做到这一点。我有一个用 phalcon 构建的网站。一切都很好,现在我偶然发现了一个问题,这就是我需要的。

当用户点击其他用户创建的帖子时。它把他带到了这个帖子,里面有照片和他输入 DB 的所有东西。我希望在浏览器中,此视图的名称不像 www.website.com/posts/index,而是像 www.website.com/posts/Nameofthepost,并且对于网站上的其他帖子也是如此。这样所有帖子(真正的广告)都会在浏览器中显示他们的名字。我希望我写的一切都可以理解。

感谢所有建议

【问题讨论】:

    标签: php phalcon volt phalcon-routing


    【解决方案1】:

    这与路由有关,不是吗?我用自己的代码修改了这个,我使用了分组,你不必这样做。我没有测试这段代码。

    // routes.php
    
    $router = new \Phalcon\Mvc\Router();
    $router->setDefaultModule("__YOUR_MODULE__");
    $router->removeExtraSlashes(true);
    
    ... your other routes ...
    
    // posts group
    
    $posts = new \Phalcon\Mvc\Router\Group(array(
        'module' => '__YOUR_MODULE__',
        'controller' => 'posts',
        'action' => 'index'
    ));
    
    // All the routes start with /post
    $posts->setPrefix('/post');
    
    $posts->add('/{postName}/:params', array(
        'action' => 'index',
        'params' => 2
    ));
    
    // Maybe this will be enough for your needs, 
    // the one above has a catch all params, which
    // has to be manually parsed
    $posts->add('/{postName}', array(
        'action' => 'index',
    ));
    
    $posts->add('[/]*', array(
        'action' => 'index',
    ));
    $router->mount($posts);
    unset($posts);
    
    ... other routes ...
    
    return $router;
    

    在您的控制器上,您可以通过这种方式获取postName 参数:

    $this->dispatcher->getParam('permaPath');
    

    如 phalcon 路由文档中所示,您可以在路由配置中使用正则表达式,像这样?

    $posts->add('/{postName:[-0-6_A-Za-z]+}/:params', array(
        'action' => 'index',
        'params' => 2
    ));
    

    所以,只有-_0-9A-Za-z 允许postName。如果 URL 中有逗号或其他内容,则路由不匹配,找不到 404 页面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-29
      • 1970-01-01
      • 2020-12-05
      • 2019-01-24
      • 2013-04-03
      • 1970-01-01
      • 2016-12-26
      • 2011-07-31
      相关资源
      最近更新 更多