cake php AppController 和继承
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我成功地做到了这一点。我有一对共享一些方法的控制器,但应用程序中的其他控制器没有(也不能)使用这些方法,因为模型中必须存在某些数据。此外,包含共享方法的控制器不应该被允许在 URL 中调用,这是一个额外的挑战。
在控制器文件夹中创建一个名为shared_controller.php的控制器。在其中定义一些共享方法,并确保它扩展AppController:
我的子控制器正在扩展 AppController。更改此设置以扩展 SharedController。我无法让共享控制器自动加载,所以我在上面添加了一个 App::import :
组件和助手必须手动合并到它们的父变量中;否则子类将覆盖它们。在每个子控制器中定义一个 __construct 方法,并对组件、帮助器和其他任何东西调用 array_merge。最后调用父构造函数。注意:这不能在 beforeFilter 中完成
如果你调用 http://example.com/shared/method< /a>,您可能会收到一条错误消息,指出您缺少数据库表或缺少视图。我不需要直接调用 Shared 中的任何方法,因此我定义了一个仅重定向到主页的路由:
希望对某人有帮助!
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.
Create a controller in your controllers folder called shared_controller.php. Define some shared methods in it, and make sure it extends AppController:
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:
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
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:
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 defineprotected $_mergeParent = 'YourParentClass';
. By default this is set to 'AppController'.Source: CakePHP 2.4 Source Code
我认为您的代码中可能有问题。如果您在
/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
中编写自己的自定义基本控制器,然后按如下要求使用它
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
then use it by a requirment like
require_once(APP."yourown_app_controller.php");
in the child contrller file.