0

我遇到了老师要求我修改的网络应用程序的问题,问题在于我对 cakePHP 不了解,我一直遇到麻烦。阅读了很多之后,我想我已经掌握了框架的基础知识。我现在的问题是我在视图中有一个链接,所以我在控制器中调用一个函数来从模型中检索数据,问题是每次我尝试访问该函数时,应用程序让我登录,以及这个想法它不应该。我不确切知道 cakePhp 上的会话处理是如何工作的,我请求一些帮助。控制器的代码是这样的:

<?php
class RwController extends AppController {
var $name = 'Rw';
// var $paginate = array(
    // 'Tip' => array(
        // 'limit' => 1,
        // 'order' => array(
            // 'tip.created' => 'desc'
        // ),
    // ),
    // 'Evento' => array(
        // 'limit' => 1,
        // 'order' => array(
            // 'evento.fecha' => 'desc'
        // ),
    // )
// );
function map() {
    $this->helpers[]='GoogleMapV3';
}
function pageForPagination($model) {
    $page = 1;
    // $chars = preg_split('/model:/', $this->params['url']['url'], -1, PREG_SPLIT_OFFSET_CAPTURE);
    // #print_r($chars);
    // if(sizeof($chars) > 1 && sizeof($chars) < 3) {
        // #echo "Belongs to ".$model.": \n";
        // #echo var_dump($chars);
    // }
    // $params = Dispatcher::parseParams(Dispatcher::uri());
    // echo "<p>".var_dump($params)."</p><br />";
    #echo $this->params['named']['model'].$model;
    #echo $this->params['named']['page'];
    $sameModel = isset($this->params['named']['model']) && $this->params['named']['model'] == $model;
    $pageInUrl = isset($this->params['named']['page']);
    if ($sameModel && $pageInUrl) {
        $page = $this->params['named']['page'];
    } else {
        #echo var_dump($this->passedArgs);
    }
    $this->passedArgs['page'] = $page;
    return $page;
}

function index() {
    $this->log('indexeando esta chingadera','debug');
    $this->loadModel('User');
    $this->loadModel('Evento');
    $this->loadModel('Tip');

    $dataEvento = $this->Evento->find('all');
    $dataTip = $this->Tip->find('all');

    $page = $this->pageForPagination('Evento');
    $this->paginate['Evento'] = array(
        'contain' => false,
        'order' => array('Evento.fecha' => 'desc'),
        'limit' => 1,
        'page' => $page
    );
    $dataEvento = $this->paginate('Evento');

    $page = $this->pageForPagination('Tip');
    $this->paginate['Tip'] = array(
        'contain' => false,
        'order' => array('Tip.created' => 'desc'),
        'limit' => 1,
        'page' => $page
    );
    $dataTip = $this->paginate('Tip');

    $this->set('users', $this->User->find('all'));
    $this->set('eventos', $dataEvento);
    $this->set('tips', $dataTip);
    $this->set('rw');

    if(isset($this->params['named']['model'])) {
        if (strcmp($this->params['named']['model'], 'Evento') == 0) {
            if($this->RequestHandler->isAjax()) {
                $this->render('/elements/ajax_rw_evento_paginate');
                return;
            }
        } elseif (strcmp($this->params['named']['model'], 'Tip') == 0) {
            if($this->RequestHandler->isAjax()) {
                $this->render('/elements/ajax_rw_tip_paginate');
                return;
            }
        }
    }
}

function about($id = null) {
    $this->Rw->recursive = 0;
    $this->set('rw', $this->paginate());
}

function beforeFilter() {
        parent::beforeFilter(); 
        $this->Auth->allow(array('index', 'about'));
}

function getCentros($id){
    $this->loadModel('Centro');
    $this->log('getcentros','debug');
    if( sizeof($id) > 1){
        $this->set('centros', $this->Centro->query("SELECT centros.id, name, latitud ,longitud 
                                                        FROM `centros`,`centrosmateriales` 
                                                            WHERE centros.id = centro_id 
                                                                AND material_id ='".$id[0]."' 
                                                                    OR material_id='".$id[1]."'"));
    }elseif( sizeof($id) >0) {
        if($id == 0){
            $this->set('centros', $this->Centro->find('all'));
        }else{
            $this->set('centros', $this->Centro->query("SELECT centros.id, name, latitud ,longitud 
                                                            FROM `centros`,`centrosmateriales` 
                                                                WHERE centros.id = centro_id 
                                                                    AND material_id ='".$id[0]."'"));
            }
        }
        $this->redirect(array('action' => 'index'));
}


}   
?>

编辑:

我调用的函数是getCentros()。

这就是我在 app_controller 中所拥有的。

<?php 
class AppController extends Controller {
    var $components = array('Session', 'Auth', 'RequestHandler');
    var $helpers = array('Html', 'Form', 'Time', 'Session', 'Js', 'Paginator', 'GoogleMapV3');
    function beforeFilter() {
        $this->Auth->userModel = 'User';
        $this->Auth->fields = array('username' => 'email', 'password' => 'password');
        $this->Auth->loginAction = array('admin' => false, 'controller' => 'users', 'action' => 'login');
        $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'index');
    }
}
?> 
4

2 回答 2

0

不知道您要调用哪个函数,但如果 cake 尝试让您登录并且您不希望将此函数添加到:

$this->Auth->allow(array('index', 'about'));

在你的前过滤器中

于 2012-11-28T08:59:51.370 回答
0

试试这个

$this->Auth->allow(array('*'));

它允许您访问控制器内部的所有功能。

但在此之前,请确保您可以访问控制器而不会出现错误,因为您的控制器名称是这样的

class RwController extends AppController {

}

可能是这样的

class RwsController extends AppController {

}
于 2012-11-28T09:52:39.447 回答