CakePHP: Overrride Controller method with plugin - cakephp

I'm new to this framework and looking to extend/override a Controller method with a plugin. If this is the wrong way to do this please let me know. I just want the solution to be modular in that there is no tampering with the extended controller. This is what I'd like..
MyController extends AppController{
function index(){}
}
I have a plugin
MyPlugin extends MyController{
function index(){}
}
I want when MyController->index() gets called it runs MyPlugin->index(). Any help would be appreciated.

What you're asking for is some kind of "reverse-inheritance", which is not supported in PHP (one might consider Aspects in Java or ObjectiveC's categories).
As far as I understand, you have the following situation:
"3rd Party App's Controller class" extends "CakePHP's Controller class"
You get both classes from external upstreams and want to modify the 3rd Party App's Controller without sacrificing either upstream. This is not possible by language, and as far as I can tell CakePHP offers no framework solution.
You might be able to achieve your goal by modifieng the routing, i.e. redirect requests to MyController to your custom MyPlugin controller and call MyController's action from there.

Related

Laravel : Should I extend my User class to 'Authenticatable'?

Im building an app with AngularJS and Laravel. I'm currently working on a login page.
By default in Laravel, the 'out of the box' class User extends 'Authenticatable' but I would like to know if I can rather simply extend it to 'Model', because I actually want to rule my authentication access through AngularJS routing and not via Laravel. I don't know if this makes sense.. and if this would even work (I dont wanna break the logic behind Laravel). Could anyone help me on that?
The Authenticatable refers to Illuminate\Foundation\Auth\User which in turn includes some Traits and implements some contracts, depending on whether you're going to use Laravel's Auth system it seems like you can omit extending this class without any problems.

Can AngularJS inject named instances of factories or services?

I am just starting with Angular with a project that seems really suited to it. In this project i must make several instances of a form builder so i have decided to make a service out of the form builder code. All is fine and good up until i take in account the other requirement: Form previews.
I was thinking of something along the lines of what this guy does but then i realised using his approach implies i cannot store/share more than one form (At least with the default injection approach used by Angular) so i gave it some thought and decided that having one instance of the FormBuilder service for each form would cut it. How can i control which instance of my service is injected into my controllers?
Propably you would not inject a specifc FormBuilder instance rather than a InstanceRegistry?
Create a service which returns a function taking the name to resolve and returns the resolved instance. This will be called from your controller.
Optionally, you could create resolves for the route if you are using either ngRoute or ui.router and do the resolving in the resolve block.
.when("...",{
controller:"...",
templateUrl:"...",
resolve:{
form1:["$formFactory", function($formFactory){
return $formFactory("form1")
})
}
})
HTH

Best Practice for Adminpanel with Acl?

I´m just working with Cakephp for a few days and I´m very impressed. But now I´m trying to get closer with Acl, but it´s a bit confusing.
My situation is, that I want to create a website with a frontend and a backend. But I´m not sure if I really need Acl for this, cause all Pages should be available for all users, except the backend of course. The tutorials in the Cookbook aren´t very helpful due to the fact, that it´s all about creating users, and groups and roles and creating the right views for login, adding and editing users, etc.
But I just need information about what Acl handles? Does it restrict the use of controllers or models?
Or do I need something else than Acl? Maybe it´s easier to check a session variable and redirect direct into the controller if the check false?
Hopefully you can bring me on the right way,
thanks in advance and best greetings from Germany,
Sascha
I suggest you to read this chapter and use the Auth component instead of simply accessing the session as you're teased to do.
http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html
For your admin backend use prefix routing.
http://book.cakephp.org/2.0/en/development/routing.html#prefix-routing
In conjunction with auth this is pretty easy to check and implement in the isAuthorized() callback.
If you don't need various 'levels' of permissions; i.e. any logged-in user is allowed to access the backend, it's best to skip ACL (for now). If, in a later stage, ACL is required, you can always add it later.
You can start with 'simple' authentication. This chapter in the cookbook describes how to do so;
http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html
In general, do not develop features that you don't need now. E.g. implementing ACL because you might need it in the future is only overcomplicating your development and chances are, they don't fit the requirements when that moment arrives.
Unlike #burzum, I'm not a big fan of prefix routing (only for very simple projects), because you'll end up mixing front- and backend actions and logic in the same controller.
I would advice to create separate controllers for the backend, possibly develop them as a Plugin. Either way, you may consider to create 'base' Controllers and Models for the backend and have your backendcontrollers/models extend them. This way you'll be able to define components/behaviors to use for the backend in 1 location. Also, by loading the 'Auth' component only in Backend controllers, you don't have to 'allow' actions in each controller in the frontend
for example;
class BackendCoreController extends AppController {
// only load the Auth component in backend controllers
// regular/frontend controllers don't require authentication
$components = array('Auth');
}
class PageAdminController extends BackendCoreController {
}
For considerations on developing the backend as a plugin, see my answer here:
Best way to implement admin panel in CakePHP

CakePHP Shell, how to use a controller action

Using cake 2.1.1. I'm trying to make a cron job to execute an action from a controller. Which is right way to do this? I have the OffersController with an action called admin_test. I'd wish to run this action every 2 hours. For the moment I made a shell in app/Console/command/SyncapiShell.php:
class SyncapiShell extends AppShell {
public $uses = array('Offer');
public function main() {
$this->Offer->admin_test();
}
}
But I get a SQLSTATE[42000] Syntax error or access violation trying to execute the shell.
I'm also using the admin routing, the auth component and ACL. How does the shell work? It ignores the authentication and the acl rights? Normally the admin_test action may be accessed only by specific authenticated users.
Thank you
No this is not the right way to do but more likely a strong indicator that your app architecture is not good. You should have fat models and tiny controllers.
Shell:uses will work like the uses property of a controller and load models. In Controllers you should use the model associations and not load thousands of models using uses.
Refactor your controller method and move the code into the Offer model.
And why does a shell need authentication or admin routing? A shell is, as the name says, a shell program, NOT a website. Authentication is basically done by the OS and user who runs the script. Only people who have access to the shell will have be able to run it anyways.

Inherit a class present in plugin to the app controller in cakephp

I want to Inherit a controller present in plugin of cakephp in my app/xyz_controller class.Is it possible.Can somebody help me.
If you need to use some class functionality you can import it but inheriting a class in a controller is not a good practice, as PHP doesn't support multiple inheritance you'll lose all functionality Controller class provides.

Resources