Creating Static pages in Cakephp

How to create Static pages in cakephp?

Well its quite simple to create static pages in cakephp but what's the better approach? Ya sure there are two possible ways, as I think. The one is Just static content to a static page and other way is creating a simple ctp file and serving all contents with database(may be also used static with if else type) .  The second approach is better because its has only one ctp file and serving to all contents. So we go 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
    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('' => $id))));
         $this->Page->id= $id;
         $this->set('title_for_layout', $this->Page->field('title'));

Step 2: Add a table `pages` 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'];?>

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

Then you can use it in your view file  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));

In case you want to change your theme/layout dynamically for different pages like contact us, about us etc pass layout and theme name in connect

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

Now change your link code

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

Here routes.php will automatically manages the requests to correct controller and action.

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.

Post a Comment