controller - cakePHP project, orgazization of folders, pages, functions -


im new cakephp, , im wondering how 'live' site this.

i see 2 possibilities :

1) there 1 controller bunch of pages (functions) in (extended) appcontroller.

2) there many controllers, each small number of pages (functions) in (extended) appcontroller.

(you question already, im going in way too)

should put contact page in separate controller blog page? (i have hunch answer yes.) why?

you don't need create controller everything. in fact, shouldn't, because there better ways around it. more static pages have, more out of hand can get.

for static pages

copy pages_controller.php cake/libs/controller folder on app/controllers folder. add following piece of code display() action:

function display() {      ...      $page = inflector::slug($page);     if (method_exists($this, $page)) {         $this->$page();     }      $this->render(join('/', $path));     return; } 

then, modify routes.php file add various static pages:

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

now, contact form static page, has logic attached it. so, can head on pagescontroller , create action (or other page isn't merely static):

function contact() {     if (!empty($this->data)) {         ...     } } 

basically, route directs static page request pagescontroller's display() action. display action checks if method of same name exists. if does, executes action , displays pages/{page}.ctp view.

for non-static pages, eg. blog

now, needs model. in fact, multiple models (post hasmany comment, post habtm tag). in order manipulate , access these different models, it's better place code separate controller.

a lot of people name controllers based on urls. example, name controller blogcontroller if want url such /blog.

a method prefer using routing urls want, , keeping controllers named per cakephp conventions.

eg. postscontroller control post model , related models. if wanted /blog url display list of posts, write route point /posts/index.

router::connect('/blog', array('controller' => 'posts', 'action' => 'index')); 

you can have additional routes too. example: /blog/2010/06/10/whats-in-a-post point /posts/view/.

again, example of what's possible. in end, should stick methods think helps keep code organized both , team.


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -