简体   繁体   中英

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

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM