简体   繁体   English

蛋糕php AppController和继承

[英]cake php AppController and inheritance

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....... 在cakephp的许多Acl组件教程中,我得到了在AppController或继承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........ 当我在AppConroller中使用上面的行时,我无法使用Auth或Acl组件...但是当我在所有子类中使用相同的代码时它工作得很好........

what will be the problem 会是什么问题

here is my appController 这是我的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 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 当我评论第4行时,这将是取消注释的错误,它提前工作正常

any help will be appreciated. 任何帮助将不胜感激。

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. 此外,不应允许在URL中调用包含共享方法的控制器,这是一个额外的挑战。

  1. Create a controller in your controllers folder called shared_controller.php. 在controllers文件夹中创建一个名为shared_controller.php的控制器。 Define some shared methods in it, and make sure it extends AppController: 在其中定义一些共享方法,并确保它扩展AppController:

     class SharedController extends AppController { 
  2. My subcontrollers were extending AppController. 我的子控制器正在扩展AppController。 Change this to extend SharedController instead. 更改此选项以扩展SharedController。 I couldn't get the shared controller to autoload, so I added an App::import above that: 我无法让共享控制器自动加载,所以我在上面添加了一个App :: import:

     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. 在每个子控制器中定义__construct方法,并在组件,帮助程序和其他任何内容上调用array_merge。 Call the parent constructor at the end. 最后调用父构造函数。 Note: this cannot be done in beforeFilter 注意:这不能在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. 如果您调用http://example.com/shared/method ,则可能会收到错误,指出您缺少数据库表或缺少视图。 I didn't need any methods from Shared to be called directly, so I defined a route to just redirect to the homepage: 我不需要从Shared直接调用任何方法,所以我定义了一个只重定向到主页的路由:

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

Hope that helps someone! 希望有人帮助!

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'; 如果有人想知道为什么他们的$components$helpers覆盖而不是在更改继承后合并是因为你需要定义protected $_mergeParent = 'YourParentClass'; . By default this is set to 'AppController'. 默认情况下,它设置为“AppController”。

Source: CakePHP 2.4 Source Code 来源: CakePHP 2.4源代码

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 : 我认为有可能是在您code.If什么不对您添加的行/app/cake/libs/controller/app_controller.php ,每个子类应该可以使用components.See 在食谱有关app_controller

CakePHP merges the following variables from the AppController to your application's controllers: $components,$helpers,$uses CakePHP将以下变量从AppController合并到应用程序的控制器: $components,$helpers,$uses

EDit @deceze 编辑@deceze

you may write your own customized base controller in /app/yourown_app_controller.php 您可以在/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"); 然后按照require_once(APP."yourown_app_controller.php");这样的要求使用它require_once(APP."yourown_app_controller.php"); in the child contrller file. 在孩子控制器文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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