是否可以动态更改视图名称或创建一个在 phalcon 中尚不存在的视图?

Is it possible to dynamically change view name or create a view that does not exist yet in phalcon?

我想知道如何在 phalcon 中执行此操作。我有一个用 phalcon 构建的网站。现在一切正常,我偶然发现了一个问题,这就是我需要的。

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

感谢所有建议

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

// 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
));

因此,postName 只允许 -_0-9A-Za-z。如果 URL 中有一个逗号之类的,那么路由不匹配,找不到 404 页面。