开发者

CakePHP - Action not defined in controller

开发者 https://www.devze.com 2023-03-22 17:32 出处:网络
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.

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';
    开发者_JS百科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)

0

精彩评论

暂无评论...
验证码 换一张
取 消