cakePHP 中的动态页面

发布于 2024-10-29 03:53:33 字数 697 浏览 1 评论 0原文

所以我一直在使用 cakePHP 创建我的第一个客户端网站,但遇到了问题。

我正在尝试创建一个类似于 WordPress 的系统,您可以在其中创建新页面(简单的标题、slug 和内容),并将它们提供给它们的 slug 地址(即“关于”将在 mysite.com/about 上提供)。

我创建了自己的控制器和“页面”模型(覆盖核心页面控制器),并设置了简单的功能(view、admin_add、admin_delete)。我的模型很简单,只有 $name ,这样它就可以连接到数据库。

我很确定我的问题出在 config/routes.php 中。这是我当前正在使用的代码:

App::import('model', 'Page');
$Page = new Page();
$pages = $Page->find('list', array('fields' => array('id', 'slug')));
Router::connect('/:pages', array('controller' => 'pages'), array('Page' => implode($pages, '|')));

但它不起作用。当我访问我拥有的页面(即 mysite.com/newpage)时,它告诉我找不到 newpage 控制器。

请帮忙!我的截止日期很紧:)

谢谢,

~harley

So I've been creating my first client website with cakePHP, and have run into a problem.

I'm trying to create a system similar to WordPress where you can create new pages (simply title, slug and content), and they are served up to their slug address (i.e. About will be available at mysite.com/about).

I've created my own controller & model for 'Pages' (overwriting the core pages controller), and have set up simple functions (view, admin_add, admin_delete). My model is simple, just the $name so it can connect to the db.

I'm pretty sure my problem lies in config/routes.php. Here is the code I'm currently using:

App::import('model', 'Page');
$Page = new Page();
$pages = $Page->find('list', array('fields' => array('id', 'slug')));
Router::connect('/:pages', array('controller' => 'pages'), array('Page' => implode($pages, '|')));

It just doesn't work though. When I visit an page I have (i.e. mysite.com/newpage), it tells me the newpage controller can't be found.

PLEASE HELP! I'm on a tight deadline :)

Thanks,

~harley

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对你的占有欲 2024-11-05 03:53:33

您需要扩展 CakeRoute 类。将您的自定义模型代码放在那里,然后将该类名称传递给routes.php 中的路由定义。routes.php

看起来像这样。

App::import('Lib', 'routes/MyCustomRoute');
Router::connect('/:page', array('controller'=>'pages', 'action'=>'display'), array('routeClass' => 'MyCustomRoute'));

然后在 libs/routes/my_custom_route.php

class MyCustomRoute extends CakeRoute {
function parse($url) {
    $params = parent::parse($url);
    //import your model
    App::import('Model','Page');
    //create model object
    $Page = new Page();
    //find using $params['page'];
    if($Page->find('first', array('conditions'=>array('page.slug'=>$params['page'])))){
         //return $params if successfull match 
       return $params
    } else 
       return false;
    //return false to fall through to next route.

}

You need to extend the Class CakeRoute. Put your custom model code in there, and then pass that class name to your route definition in routes.php

routes.php would look something like this.

App::import('Lib', 'routes/MyCustomRoute');
Router::connect('/:page', array('controller'=>'pages', 'action'=>'display'), array('routeClass' => 'MyCustomRoute'));

Then over in libs/routes/my_custom_route.php

class MyCustomRoute extends CakeRoute {
function parse($url) {
    $params = parent::parse($url);
    //import your model
    App::import('Model','Page');
    //create model object
    $Page = new Page();
    //find using $params['page'];
    if($Page->find('first', array('conditions'=>array('page.slug'=>$params['page'])))){
         //return $params if successfull match 
       return $params
    } else 
       return false;
    //return false to fall through to next route.

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文