8vius
8vius

Reputation: 5836

CakePHP - Action not defined in controller

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'));

Upvotes: 0

Views: 7768

Answers (2)

Will Parsons
Will Parsons

Reputation: 36

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'));

Upvotes: 2

Anh Pham
Anh Pham

Reputation: 5481

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)

Upvotes: 1

Related Questions