CakePHP Calling a Plugins Model in the App's Controller - cakephp

I am building a plugin and have certain methods in the Model of the Plugin.
From the Controller of the App lets say Users Controller i wanna call a Model from the Plugin.
How do i access the Plugins Model ?

$this->loadModel('PluginName.ModelName');
whats the big deal?

Related

Is a service a model in Angular?

I create a project structure of a web-app with angular.
I create my directories with services, controllers and views.
Then, my team ask me: where are the models? (Nobody of us have worked with angular)
And I answered that in the service, because is where the data persist.
But Im no really sure, and I cant find useful info about this.
What you are looking for is a factory provider. You can create a factory with a collection inside, and this "class" has operations to handle the collection and sincronize it with the back-end.
See this example:
https://github.com/AngularClass/angular-websocket/blob/master/README.md
You can see how to create a collection and initialize it with data that comes from a websocket.
Aditionally you can create operations to handle the collection
This could be consider a "model" in angular.
It is a best practice to keep business logic in Services (and not in controllers, for example). This is, among other reasons, because controllers are instantiated for each view and Services are singeltons.
You can refer to those great posts for more details:
https://johnpapa.net/sharing-data-in-an-angular-controller-or-an-angular-service/
http://teropa.info/blog/2014/10/24/how-ive-improved-my-angular-apps-by-banning-ng-controller.html
https://toddmotto.com/rethinking-angular-js-controllers/
I'm going to assume from the verbiage you've used that you're referring to Angular 1.x.
Services are typically used by controllers to pull data from a provider not displayed in the view. The provider can be a public api, a static file on your own server, a mongo database, etc.
A model for a controller represents the data being manipulated between the view and the controller (refer to ngModel).

Use CakePHP applications layout in plugin

I am writing a CakePHP plugin which does not have its own layout but utilises the layout of the application. I tried achieving this by just not creating the /Layout folder in the plugin BUT if i do that the controller crashes with a "Missing Route" error.
All tutorials are showing me how to use a plugin's Layout in my CakePHP application, but i want the opposite.
I am using CakePHP 3.X.
if you don't want to create a view file for the controller method, then just try using line below in that controller method.
$this->autoRender = false;

Best way to implement admin panel in CakePHP

I am trying to move from CodeIgniter to CakePHP and can't figure out the best way to implement an admin panel. In CI I would create two different applications, one for the frontend and one for the admin panel.
After Googling around, I have found three ways to implement admin panel in CakePHP:
Routing - I don't want to use this as I want by Controllers/Models to be separate for frontend and admin panel
Plugin
Two separate apps
Should I use plugin to implement admin panel or should I have separate apps? Any benefits of one over the other?
I normally develop the admin/backend as a plugin. This keeps your backend/admin controllers/views/models separated from the frontend and you don't have to jump through hoops to have separate stylesheets, layouts etc.
Another advantage is that both front- and backend are still part of the same application, so if desired, you can share logic/components, for example you'll be able to put helpers that are usable both for front- and backend in another plugin (e.g. plugins/Shared or plugins/Handytexttools) and use those both wherever you want
As a rule of thumb; put components that may be reuseable for other projects in a separate plugin, this way you can just add those plugins to other projects without problems. Keep your plugins simple; it's no problem to create a plugin containing just one or two helpers or models and a few files of JavaScript. This will make it easier to 'cherry pick' the plugins that you need for a project. Once Cake has 'cached' the file-locations of all classes in your plugins, the overhead of separate plugins should be minimal.
Coming back to the 'admin' plugin. Try to only include code specific for this project in your admin plugin and reusable parts in another one (e.g. Generic stylesheets and layouts for admin-panels). You'll be able to start a admin-plugin for your next project with minimal coding
Good luck with your project and enjoy CakePHP
If you want to keep your controllers and models separate - I'd go with a separate app, although you'll end up with a bunch of duplicate code between the apps (maintenance headache waiting to happen).
My choice would be admin routing and an admin theme.
Enable admin routing in /app/Config/core.php
In AppController beforeFilter():
$this->theme = isset($this->params['admin']) ? "Admin" : "Site";
Move all your site views and assets into /app/View/Themed/Site/
Create your admin themes in /app/View/Themed/Admin
Old and refers to CakePHP 1.3, but still is a question you should check: CakePHP admin panel
The Cake way is routing. I'd go with a plugin like CakeDC Users that makes things easier.
You could use admin-routing. Check out:
http://book.cakephp.org/2.0/en/development/routing.html#prefix-routing
Another solution -which I find really easy to implement- is like this:
In your AppController:
public function beforeFilter(){
$this->set('current_user', $this->Auth->user());
}
This makes the $current_user available in your app.
Then in your view-files, you can check:
<?php if ($current_user['role'] == 'admin'){/*place code for admin users to see here*/} ?>
<?php if ($current_user){/*place code for logged-in users to see here*/} ?>
I know this is an old thread. But would like to ask if anyone had trouble implementing the admin panel as a plugin. Particularly duplication of code.
For example you're implementing an e-commerce site. You have an OrderController both in the main and admin plugin. Don't you think it's kinda hard to maintain the logic in two places?
How about just using one main controller. It's serves two purpose. One as an API then the controller for your Admin webapp.
Your public side would then basically communicate via API to fetch data.
Do you think it's a good idea?
You can use admin views like admin_index.ctp just change this
//Configure::write('Routing.admin', 'admin');
to
Configure::write('Routing.admin', 'admin');
in core.php and in the controller add admin_index() function

CakePHP: Overrride Controller method with plugin

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.

Plugin default controllers in CakePHP 2.0

I'm struggling to get a custom plugin to work with a default controller:
e.g. in CakePHP 1.3 I could create a users plugin and create a users_controller in it which automatically becomes the plugins controller,
I could access the methods of the user controller in the users plugin via:
/users/add
/users/edit/1
If I do the same in CakePHP 2.0 I get the following errors:
Error: Users.AddController could not be found.
Error: Create the class AddController below in file: /home/richarda/www/test/cake_zero/www/app/Plugin/Users/Controller/AddController.php
I can access them at the following urls:
/users/users/add
/users/users/edit/1
Oddly, the default index action works as expected, ie. I can go to
/users
and can see the index view from the users controllers in the users plugin.
There is no mention of default controllers for plugins in the 2.0 docs, has this functionality been removed?
Turns out default routing for plugins has been disabled in CakePHP2.0
Here's the ticket I posted: http://cakephp.lighthouseapp.com/projects/42648/tickets/2237-20-plugins-dont-have-a-default-controller#ticket-2237-3
The solution is to create a custom route:
Router::connect('/users/:action', array('controller'=>'users', 'plugin'=>'users');
And you're good to go.
Hope this helps someone.
I use this in cake 2.2.0 and it works for my plugin called admin.
Hope you can apply it to your situation.
Router::connect('/admin/', array('plugin'=>'admin','controller'=>'groups','action'=>'index'));

Resources