存根,cakePHP 中的动态主页

发布于 2024-09-28 08:58:43 字数 689 浏览 8 评论 0原文

我正在为一位客户开发一个网站,该客户希望能够更新修改其内容。简述是允许他们编辑页面,但不能创建或删除页面。对于这个网站,我决定使用 cakePHP,因为我听说过一些好东西。

首先,快速解释一下我的设置。我有一个名为“内容”的表,其中存储每个页面的内容。该表有一个 pid、一个 varchar“标题”、一个 varchar“slug”和一个长文本“正文”。它们都很不言自明,每个页面都有自己的行,正文将是一个简单的 HTML 转储。

我遇到了两种情况。首先,是设置主页。 Cake的默认页面是基于home.ctp的页面,但它是静态的。目前我作为主页的页面位于 localhost/alc/contents/view/2。我知道这与路由有关,但是当我需要每个细节时,大多数示例都给出了一半的解决方案:P

第二个问题是页面的slugs。当前每个页面都位于 /contents/view/id 下,我希望将其作为数据库中的 slug。每次我尝试更改此设置(即修改索引中的视图链接)时,我都会收到错误而不是页面内容。

对此的任何帮助将不胜感激,因为有两件事我似乎无法正确掌握。谢谢!

顺便说一句,您可以在 http://www.roberttilt.name/web- 查看该网站开发/ALC_proto/

I'm working on a site for a client that wants to be able to update modifying their content. The brief was to allow them to edit pages, but not create or delete them. For the site I decided to work with cakePHP, as I've heard good things.

First up, a quick explanation of my setup. I've got a single table, called 'contents', in which i'm storing each page's content. The table has a pid, a varchar 'title', a varchar 'slug' and a longtext 'body'. They're all pretty self explanitory, Each page will have it's own row, and body will be a simple HTML dump.

I've got two situations that I am having trouble with. Firstly, is setting the homepage. Cake's default is the page based on home.ctp, but that is static. Currently the page I was as the homepage is at localhost/alc/contents/view/2. I understand this is something to do with the routing, but most examples out there give half the solution, when I need every detail :P

The second problems is the slugs of the pages. Each page is currently under /contents/view/id, and i'd like this to be the slug in the database instead. Each time i try to change this (i.e. modify the view link in my index), I get an error rather than the page's content.

Any help on this would be appreciated, as there are the two things I cannot seem to grasp properly. Thanks!

By the way, you can view the site at http://www.roberttilt.name/web-dev/ALC_proto/

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

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

发布评论

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

评论(1

泅渡 2024-10-05 08:58:43

对于第一个问题,您需要打开 /app/config/routes.php 并更改主页的行。即:

Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));

需要成为

Router::connect('/*', array('controller' => 'contents', 'action' => 'view'));

在您的控制器文件 /app/controllers/contents_controller.php 中转到操作视图并将其更改为接受空 id ie

function view($id = null){
   if($id == null){ //Load the default home page
      $this->find('first', array('conditions'=>array('default'=>1)));
   } else {
      //load the 
      $this->find('first', array('conditions'=>array('OR'=>array('slug'=>$id, 'id'=>$id))));
   }
   .....
}

这样,如果未提供 id 或 slug,您将加载主页。如果加载其中之一,只需使用它来加载所需网页的内容即可。

您的链接将如下所示:

$this->Html->link('About', array('controller'=>'contents', 'action'=>'view', $slug_var));

链接将转换为

<a href="/your-nice-slug">About</a>

也许您必须查看Cookbook

For the first question you need to open the /app/config/routes.php and change the line which is for the home page. i.e.:

Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));

need to become

Router::connect('/*', array('controller' => 'contents', 'action' => 'view'));

In your controller file /app/controllers/contents_controller.php go to action view and change it to accept empty id i.e.

function view($id = null){
   if($id == null){ //Load the default home page
      $this->find('first', array('conditions'=>array('default'=>1)));
   } else {
      //load the 
      $this->find('first', array('conditions'=>array('OR'=>array('slug'=>$id, 'id'=>$id))));
   }
   .....
}

This way if the id or slug are not provided you are loading the home page. If one of them is loaded, just use it to load the contents of the desired web page.

Your links will look like:

$this->Html->link('About', array('controller'=>'contents', 'action'=>'view', $slug_var));

The link will be converted to

<a href="/your-nice-slug">About</a>

Probably you have to take a look on the Cookbook.

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