Tuesday, November 10, 2009

Create a Static Page, Set a Page Title Like About us Page using CakePHP

Let me try to create an 'About us' page with CakePHP. This is rather simple. This will explain how to create ANY static page with Cake (short form of CakePHP).
Also near the end, I'll tell about you about how to give your page a custom title using

$this->pageTitle = 'This is your page title'; 

To create About us page (Static Page Creation Example)
Just create a new file.
Write some text. You can use ANY HTML as well.
Save it as 'about_us.ctp' under 'app/views/pages' folder.
You are done.

You can access the page as:
http://caketest.local/pages/about_us

Important Note!
Note the word 'pages'. It is a CakePHP core controller. It displays any_name.ctp file saved  under 'app/views/pages' folder. I'll talk a lot about controller later. For the time being, it is just more than enough to know that you can create a new file, write some text, save it with a name under that path, and point your browser to:

http://caketest.local/pages/my_page_name

You should be able to view your new page.

Note again:
1. The pages MUST have a file extension of '.ctp'. (FILENAME should be underscored ('_'), like, my_file_name, to obey Cake convention.
2. Static pages MUST be saved under 'app/views/pages' folder.
3. To access the page you MUST use: http://caketest.local/pages/my_page_name

As such, you can change ANY or ALL of the above three settings in Cake. But it is better to accept default settings for most of the time - at least when we have just stepped into a new DOMAIN.


Page Title for your Static Page

Now to give this new page a custom page title copy-paste following code anywhere in 'about_us.ctp' file:

     <?php
     $this->pageTitle = 'Replace this with Your Own Title Here';
     ?>
I placed it at the top of my 'about_us.ctp' file for natural reason.

And here is my NEW Static About us page:

















You can create any static page like Privacy Policies Page, Terms of Service page etc. Just FOLLOW the conventions and Cake does the rest for you.

May I move on to explore some inner details of CakePHP, like creating LINKS etc.?

Cheers!

5 comments:

Rajneesh Sharma said...

Thank you for this simple tutorial

Anonymous said...

Thanks for the good words.

Unknown said...

Can I create a controller such as pages_controller and insert some actions inside that controller ? So I also can use the url /pages/action....

Anonymous said...

I think you can. ? This is called modelless controller...

class MycustomController extends AppController{
var $name="Mycustom";
var $uses = null;
...
}

Note, you need to use $uses = null to tell CAKE this controller does not have any model.

Hope it helps.

Unknown said...

please tell me every process for creating single about us page, i follow ur steps bt its not working. do i have changes in controller.