cake php AppController 和继承

发布于 2024-08-29 04:15:13 字数 1196 浏览 8 评论 0原文

在cakephp中Acl组件的很多教程中 我收到在 AppController 或继承 AppController 的所有其他控制器中添加组件的指令......

但问题是

var $components=array('Auth',Acl);

当我在 AppConroller 中使用上面的行时,我无法使用 Auth 或 Acl 组件...... 但是当我在所有子类中使用相同的代码时,它工作正常......

这里的问题

是我的

<?php
class AppController extends Controller {

     var $helpers = array('Html', 'Form', 'Session','CssMenu');
     var $components = array('Auth');

     function beforeFilter() {
        //Configure AuthComponent
        $this->Auth->authorize = 'actions';
        $this->Auth->authError = "Sorry, you are lacking access.";
        $this->Auth->userModel = 'Login';
}

}
?>

usersController 的 appController 代码,

<?php
class userssController extends AppController{
    var $name="Logins";
    //var $components = array('Auth');
    var $layout='login';

    function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('*');
        $this->Auth->loginRedirect = array('controller' => 'homes', 'action' => 'index');
    }
}
?>

当我注释第 4 行时,这将出现错误 取消注释后效果很好 提前致谢

任何帮助将不胜感激。

In many tutorial of Acl component in cakephp
i got instruction that add component either in AppController or in all the other controllers which inherits AppController.......

but problem is

var $components=array('Auth',Acl);

when i use the above line in AppConroller i cant use the Auth or Acl component...
but when i use the same code in all the child classes it works fine........

what will be the problem

here is my appController

<?php
class AppController extends Controller {

     var $helpers = array('Html', 'Form', 'Session','CssMenu');
     var $components = array('Auth');

     function beforeFilter() {
        //Configure AuthComponent
        $this->Auth->authorize = 'actions';
        $this->Auth->authError = "Sorry, you are lacking access.";
        $this->Auth->userModel = 'Login';
}

}
?>

code for usersController

<?php
class userssController extends AppController{
    var $name="Logins";
    //var $components = array('Auth');
    var $layout='login';

    function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('*');
        $this->Auth->loginRedirect = array('controller' => 'homes', 'action' => 'index');
    }
}
?>

when i comment the line 4 this will b error
on uncomment it works fine
thanks in advance

any help will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自由如风 2024-09-05 04:15:13

我成功地做到了这一点。我有一对共享一些方法的控制器,但应用程序中的其他控制器没有(也不能)使用这些方法,因为模型中必须存在某些数据。此外,包含共享方法的控制器不应该被允许在 URL 中调用,这是一个额外的挑战。

  1. 在控制器文件夹中创建一个名为shared_controller.php的控制器。在其中定义一些共享方法,并确保它扩展AppController:

    class SharedController 扩展 AppController {
    
  2. 我的子控制器正在扩展 AppController。更改此设置以扩展 SharedController。我无法让共享控制器自动加载,所以我在上面添加了一个 App::import :

    App::import('控制器', '共享');
    类 SubController 扩展 SharedController {
    
  3. 组件和助手必须手动合并到它们的父变量中;否则子类将覆盖它们。在每个子控制器中定义一个 __construct 方法,并对组件、帮助器和其他任何东西调用 array_merge。最后调用父构造函数。注意:这不能在 beforeFilter 中完成

    函数 __construct(){
        $this->components = array_merge($this->components,array('Mycomponent'));
        父级::__construct();
    }
    
  4. 如果你调用 http://example.com/shared/method< /a>,您可能会收到一条错误消息,指出您缺少数据库表或缺少视图。我不需要直接调用 Shared 中的任何方法,因此我定义了一个仅重定向到主页的路由:

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

希望对某人有帮助!

I managed to pull this off. I had a pair of controllers that shared some methods, but other controllers in the application did not (and could not) use those methods because certain data had to be present in the model. Moreover, the controller containing the shared methods should not be allowed to be called in the URL, which was an extra challenge.

  1. Create a controller in your controllers folder called shared_controller.php. Define some shared methods in it, and make sure it extends AppController:

    class SharedController extends AppController {
    
  2. My subcontrollers were extending AppController. Change this to extend SharedController instead. I couldn't get the shared controller to autoload, so I added an App::import above that:

    App::import('Controller', 'Shared');
    class SubController extends SharedController {
    
  3. Components and helpers must be merged into their parent variables manually; otherwise the child classes will overwrite them. Define a __construct method in each subcontroller and call array_merge on the components, helpers, and whatever else. Call the parent constructor at the end. Note: this cannot be done in beforeFilter

    function __construct(){
        $this->components = array_merge($this->components,array('Mycomponent'));
        parent::__construct();
    }
    
  4. If you call http://example.com/shared/method, you'll probably get an error that you're missing a database table, or missing a view. I didn't need any methods from Shared to be called directly, so I defined a route to just redirect to the homepage:

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

Hope that helps someone!

如果有人想知道为什么他们的 $components$helpers 在更改继承后会覆盖而不是合并,是因为您需要定义 protected $_mergeParent = 'YourParentClass';< /代码>。默认情况下,它设置为“AppController”。

来源:CakePHP 2.4 源代码

If anyone is wondering why their $components and $helpers overwrite instead of merging after changing inheritance is because you need to define protected $_mergeParent = 'YourParentClass';. By default this is set to 'AppController'.

Source: CakePHP 2.4 Source Code

对你的占有欲 2024-09-05 04:15:13

我认为您的代码中可能有问题。如果您在 /app/cake/libs/controller/app_controller.php 中添加行,每个子类都应该能够使用组件。请参阅 < a href="http://book.cakephp.org/view/829/The-App-Controller" rel="nofollow noreferrer">cookbook 中的 app_controller:

CakePHP 将以下变量从 AppController 合并到您的应用程序的控制器:$components,$helpers,$uses

编辑@deceze,

您可以在 /app/yourown_app_controller.php 中编写自己的自定义基本控制器,

class YourOwnAppController extends Controller
{
       var $components = array("Auth");
}

然后按如下要求使用它require_once(APP."yourown_app_controller.php"); 在子控制器文件中。

I think there may be something wrong in your code.If you add the line in /app/cake/libs/controller/app_controller.php,every child class should be able to use the components.See about app_controller in cookbook:

CakePHP merges the following variables from the AppController to your application's controllers:$components,$helpers,$uses

EDit @deceze

you may write your own customized base controller in /app/yourown_app_controller.php

class YourOwnAppController extends Controller
{
       var $components = array("Auth");
}

then use it by a requirment like require_once(APP."yourown_app_controller.php"); in the child contrller file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文