Create web page in CakePHP

To create a webpage in cake:

Basically, everything you do has to be an action in a controller. An “Action” is a function in a controller.
urls are formed as follows:
http://www.site.com/path_to_cake_installation/action/controller/param1/param2/param3/param4/etc…

so if I visit:
http://www.site.com/sphere/people/view/

you’re saying that there’s a people controller with action called ‘view’. The action is a function within a controller class.
So to define a view action in a People controller, you would place the following code in sphere/app/controllers/people_controller.php:

< ?php
class PeopleController extends AppController
{
var $name = 'People';
function view()
{
// programming logic goes here.
}
}
?>

For this respective controller action to be rendered, you must create a view by placing the following code in sphere/app/views/people/view.thtml:

Hello world

The above view would be rendered by cake when the people controller is called with the view action.

SPECIAL CASES:

If you visit: http://www.site.com/sphere/people/, with no action defined, then the index() action is called. To define the index html contents, put the following in your controller:
function index()
{
}
and define the contents in index.thtml in app/views/people/

PAGES CONTROLLER:

For static pages that require no backend and that don’t really belong in a controller, there is a “PagesController” defined in cake that is accessed as follows:
http://www.site.com/sphere/pages/display/page1/
where page1 can be defined in
/app/views/pages/page1.thtml
page1.thtml can be called anything (say, poopoo.thtml) and will be accessed by visiting:
http://www.site.com/sphere/pages/display/poopoo/
I don’t know that you’ll be making any static pages though.
Either way, you can then define what’s called “routes” to allow http://www.site.com/sphere/pages/display/PAGE_NAME to be visited simply by accessing http://www.site.com/sphere/PAGE_NAME/
The route should be defined by default. So you should be able to access http://www.site.com/sphere/pages/display/poopoo/ by going to http://www.site.com/sphere/poopoo/

OTHER STUFF

Oh, and be sure to read this for how to name classes and files so that cake finds them:
http://manual.cakephp.org/appendix/conventions
and if you want to render an element in every page (say, a menu), don’t use include(), instead create an element. To see an example, look at the following files:
app/views/auth/index.thtml (and other files in this folder)
app/views/elements/authz/menu.thtml
Also, read http://manual.cakephp.org/chapter/helpers
you HAVE to use the < ?=$html->link()?> and < ?=$html->url()?> functions to do relative linking, and it saves you a bunch of typing when you get used to it (5 min learning curve).
I think that should cover everything… You should be an up and coming cake pro in about 20 mins.

Leave a Reply

You must be logged in to post a comment.