views:

251

answers:

1

Hi All, I was wondering if it's somehow possible to add another abstraction controller between AppController and my app's other controllers?

So that my controllers, e.g. UsersController extends SecureController and SecureController extends AppController.

Also I want to be able to have other controllers extend AppController directly: SomeNonSecureController extends AppController.

this is because my current AppController has all sorts of Auth and ACL stuff in its beforeFilter, but i also have controllers that don't need that security stuff (before everything needed the security, no new specs have been added).. but because some many controllers do need it, it doesn't make sense to copy-paste the code to all needy controllers.

I was thinking to but all the beforeFilter security stuff into a SecureController - that way any controllers that need security simpley extend it, while others inherit from AppController directly.

How would you go on about doing something like this?

Thanks in advance, Ken.

+4  A: 

My first thoughts would be to see if I could abstract some of the functionality from the beforeFilter into a component - remember components can use other components too, just include them in your component's $components property, so you can access the AuthComponent and AclComponent etc.

If this was not suitable then I'd go for your route, in order to do it, just include('secure_controller.php'); before your individual controller class declaration in it's file.

I have done something similar by creating a BaseController that I use in all my projects which provides all my admin CRUD actions that are standard. I then have my AppController extend this which contains application specific, controller wide functionality, then individual controllers extend that, and end up being practically empty. All I do is:

// app/base_controller.php
<?php class BaseController extends Controller {} ?>

// app/app_controller.php
<?php
include('base_controller.php');
class AppController extends BaseController {}
?>

// app/controllers/my_controller.php
<?php class MyController extends AppController {} ?>
neilcrookes
here's my problem, i need to build an app that's has 2 webroots: one /admin , and one /probut they need to be sharing the same /app folder for models and such.now everything under /admin needs to be secure with Auth and ACL, while the /pro doesn't need to be (expect for maybe a few controllers).How would you suggest going about doing this?BTW, i'm Ken from the comments on you ACL blog post :) thanks for the reply!
Ken
Hmmm, I'd probably upgrade to 1.3 then use Routing.prefixes to handle the different URL prefixes and stick to using the same models and controllers for all areas. Then use AuthComponent::allowedActions property to switch which actions required authorisation. Or have I missed the point?
neilcrookes
Well that might work (don't know anything about Routing.prefixes really) - but 1.3 is still in alpha, and this is a app for production use, so i can't risk it. need to find a 1.2.5 solution :-\
Ken