0
votes

I'm getting the missing action in controller from CakePHP, but the action home is defined in my controller and I've made an empty view for it.

<?php
class PagesController extends AppController {

    var $name = 'Pages';
    var $uses = array('Event', 'News', 'Person', 'Signup', 'Workshop', 'Course');

    function home() {
        $this->layout = 'main';
    }

    function news() {

    }

    function events() {

    }
}
?>

This is my routes file:

<?php

    Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
    Router::connect('/admin/logout', array('controller' => 'users', 'action' => 'logout'));
    Router::connect('/', array('controller' => 'pages', 'action' => 'home'));
2
What URL are you trying to access the page from? - Sam Dufel
if you modify PagesController, you need to change the default routes too. - Anh Pham
this does not look like a very good approach. the pages controller is for static content; and you should not be including all your models in the uses array like that - it's considered bad practice. - Ross
I updated my post with my routes file, it still doesn't work, don't know what the problem is - 8vius

2 Answers

2
votes

Try this:

<?php
class PagesController extends AppController {

var $name = 'Pages';
var $uses = array('Event', 'News', 'Person', 'Signup', 'Workshop', 'Course');

function display() {

    $path = func_get_args();

    $count = count($path);
    if (!$count) {
        $this->redirect('/');
    }
    $page = $subpage = $title_for_layout = null;

    if (!empty($path[0])) {
        $page = $path[0];
    }
    if (!empty($path[1])) {
        $subpage = $path[1];
    }
    $this->set(compact('page', 'subpage', 'title_for_layout'));

    switch ($page) {
        case 'home':
            $this->_home();
            $this->render('home');
        break;
        default:
            $this->render(implode('/', $path));
    }
}

function _home() {
    $this->layout = 'main';
}

function news() {

}

function events() {

}
}
?>

And place this line at the top of your routes:

    Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
1
votes

remove Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display')); in your routes.php

and modify the root path route: Router::connect('/', array('controller' => 'pages', 'action' => 'home')); (it's optional, but maybe you'll want that)