Rathika Jeganathan
Rathika Jeganathan

Reputation: 77

How to create a static page in cakephp?

Currently am creating one auction website using cakephp. It have a menu bar like about us, contact us. I have created only the default page. So i want to create those pages. advice me how to create.

Upvotes: 6

Views: 13537

Answers (5)

Rémi
Rémi

Reputation: 3967

Since the new version of cakephp is freshly out, I'm adding this answer to deal with the newer version (3.x).

To link to a static page you still use the PageController but the code slightly changed.

Here the code you would need in the 3.x version

$routes->connect('/about', ['controller' => 'Pages', 'action' => 'display', 'about']);

You can read more about the new routing system here.

I have no affiliation with cakephp. I added this answer since I found this post while searching how to do this in 3.0

Upvotes: 3

RN Kushwaha
RN Kushwaha

Reputation: 2136

Read more here

Method 1: if you want to create content pages like about us, privacy policy which contents can be changed by an admin interface follow these steps

Step1: Change pagesController

class PagesController extends AppController {
function beforeFilter() {
    $this->Auth->allow('content');//to allow to be visible for non-logged in users, if you are using login system
    parent::beforeFilter();
}
public function content($id = null, $layout = null, $theme=null) {
    if ($layout) $this->layout = $layout;//if you are using mulitple layouts and themes and want to change it dynamicaly
    if ($theme) $this->theme = $theme;

    $this->set('content', $this->Page->find('first', array('conditions' => array('Page.id' => $id))));
     $this->Page->id= $id;
     $this->set('title_for_layout', $this->Page->field('title'));

}

}

Step 2: add a table content with fields you need like id, title, content, image, theme,layout etc. Step 3: In View/Pages add content.ctp

 <div class="row innerPage">    
<div class="col-lg-12 col-md-12 col-sm-12">
  <div class="row userInfo">
    <div class="col-xs-12 col-sm-12">
      <h1 class=" text-left border-title"> <?php echo $content['Page']['title'];?> </h1>
      <div class="w100 clearfix">
        <?php echo $content['Page']['content'];?>
      </div>
    </div>
  </div>


However you can change html according to your need, I prefer bootstrap framework.

Then you can use it as

<?php echo $this->html->link("Terms of Services", array("controller" => "pages", "action" => "content", 5), array("class" => 'themeprimary','target'=>'_blank')) ?>

This will generate a link yoursite/pages/content/5. 5 is the id of row you want to show the details of.

If you want your link like yoursite/terms then you need one more step to go. In routes.php add this line.

Router::connect('/terms', array('controller' => 'pages', 'action' => 'content',5)); 

Method 2: You simply need to display content without any database Step1 :Just create a about.ctp under View/Pages and put the content you want to display Step 2: Change your pagesController. add a method about

public function about($layout = null) {
    $this->set('title_for_layout', 'About');    
}

Thats it.

Upvotes: 2

Jack
Jack

Reputation: 5768

Create an about.ctp in the /app/views/pages/ folder.

Then add Router::connect('/about', array('controller' => 'pages', 'action' => 'display', 'about')); in the /app/config/routes.php file. You should be able to access it at www.yoursite.com/about

Upvotes: 4

wordy
wordy

Reputation: 173

Old thread, but I found it while trying to do the same in 2.x.

Jack's answer is correct, with a small typo. It should be

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

Hopefully this helps someone else, as it did me.

Upvotes: 7

Ross
Ross

Reputation: 17967

You can use the pages controller for this purpose.

Creating views at APP/views/pages/ with names such as about_us.ctp and contact_us.ctp will allow you to access them at the url:

www.site.com/pages/about_us

you can then change how these URIs look with routing.

Upvotes: 0

Related Questions