Newbie Coder
Newbie Coder

Reputation: 10942

Simple examples on how to use CakePHP pages_controller.php to create static pages

I am complete beginner to CakePHP but I am a bit knowledgeable in ROR.

Can somebody pls give me some simple examples on how to make use of pages_controller.php? I want to create static pages such as Home, About, and etc but I don't know how and where to start. I tried something like creating a about.ctp in the views and creating about_controller.php (this is how being done in Ruby on Rails) but I just got some errors.

I concluded that all of the static pages will only use 1 controller which is pages_controller.php but I dont know how.

I tried reading the article found on this link:

http://book.cakephp.org/view/958/The-Pages-Controller

but it doesn't give me anything that will help me learn how to use it.

This is what I got from the page:

CakePHP core ships with a default controller called the Pages Controller (cake/libs/controller/pages_controller.php). The home page you see after installation is generated using this controller. It is generally used to serve static pages. Eg. If you make a view file app/views/pages/about_us.ctp you can access it using url http://example.com/pages/about_us

When you "bake" an app using CakePHP's console utility the pages controller is copied to your app/controllers/ folder and you can modify it to your needs if required. Or you could just copy the pages_controller.php from core to your app.

Can somebody pls show me or explain to me how??? I am a total beginner pls help.

Upvotes: 0

Views: 3951

Answers (1)

Ross
Ross

Reputation: 17967

It's pretty self explanatory.

Create a file in your APP/views/pages/ folder - e.g about_us.ctp

Type in your content. No layout; just the text, tables/images/etc

<h3>About my site</h3>
<p>bla bla la</p>
<?php echo $this->Html->image('my_img.jpg'); ?>

Save.

Go to www.site.com/pages/about_us - your page is served.

Pages is the controller to serve static pages. you don't need an about_controller, unless you need something more than just a static page.

You can change how the link looks by using routing.

You can set variables for use in your template as well:

about_us.ctp

<?php
$this->set('title_for_layout', 'My about page');
$this->set('active_link', 'about'); 
?>
<h1>My page!</h1>
etc

Upvotes: 7

Related Questions