[英]Is it possible to dynamically change view name or create a view that does not exist yet in phalcon?
I would like to know how can i do this in phalcon. 我想知道我该如何在Phalcon中做到这一点。 I have a web site build with phalcon.
我有一个使用phalcon构建的网站。 All is working great now i stumbled upon a problem, here is what i need.
现在,我偶然发现一个问题,一切都很好,这是我所需要的。
When a user clicks on a post that was created by another user. 当用户单击另一个用户创建的帖子时。 It takes him to this post with pictures and all things he entered to DB.
他将这篇图片和他输入DB的所有内容带到了这篇文章。 I would like that in browser the name of this view is not like www.website.com/posts/index but that it is like www.website.com/posts/Nameofthepost, and like that for each other postings on the website.
我希望在浏览器中,该视图的名称不像www.website.com/posts/index,而是像www.website.com/posts/Nameofthepost,并且与网站上的其他帖子一样。 So that all posts (really ads) show their name up in browser.
这样所有帖子(真正的广告)都会在浏览器中显示其名称。 I hope i wrote everything understandable.
我希望我写的一切都可以理解。
Appreciate all suggestions 感谢所有建议
That has to do with routing doesn't it? 这与路由有关吗? I modified this from my own code, I used grouping, you don't have to.
我从自己的代码中修改了此代码,使用了分组功能,您不必这样做。 I didn't test this code.
我没有测试此代码。
// 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;
On your controller, you can get the postName
param this way: 在您的控制器上,您可以通过以下方式获取
postName
参数:
$this->dispatcher->getParam('permaPath');
As shown in the phalcon routing documentation, you can use regex in your routing config, something like this? 如phalcon路由文档中所示,您可以在路由配置中使用regex,类似这样吗?
$posts->add('/{postName:[-0-6_A-Za-z]+}/:params', array(
'action' => 'index',
'params' => 2
));
So, only -_
, 0-9
, AZ
, az
allowed for postName
. 因此,
postName
只允许-_
, 0-9
, AZ
和az
。 If the URL had a comma in there or something, then route doesn't match, 404 page not found. 如果网址中有逗号或其他内容,则路由不匹配,找不到404页。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.