views:

24

answers:

1

Hey everyone.

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/

A: 

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.

Nik
Thanks for your help! Some of this worked well, Unfortunately I can;t seem to get that view function working properly, it keeps throwing when it hits the find statement. I tried to augment it so the load homepage line is $this->set('content', $this->Content->find('all',array('conditions'=>array('id'=>'1'))));It stops throwing the error, but still doesn't actually link to the homepage :(
tiltos
well, you need to have a default column in your contents table and that default field need to be boolean and for all entries in the database you need to have only one default=1 :)
Nik