Use CakePHP applications layout in plugin - cakephp

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;

Related

Cakephp 3.0.x: helpers for errors

In my error layout (src/Template/Layout/error.ctp) I'm trying to use a custom helper, but I see that CakePHP loads automatically only the Html and Form helpers.
How to load other/custom helpers in case of error?
Thanks.

How to use correctly a CakePHP plugin

I've developed a bug tracker using CakePHP 2.4.4. I made this as a standalone cakephp app but now I wanna transfer it to a plugin, in order to reuse it in other projects.
As I already read at the docs (http://book.cakephp.org/2.0/en/plugins.html), I have followed the instructions from there and created the correct folder and files structure. This is what i've done so far: https://github.com/lubbleup/BugCake/tree/plugin
But now,when i try to use the plugin in a separate cakephp installation, I cannot understand how to make of the plugin and, for example, use it's controllers and functionality etc.
can anyone help me out here?
ps:this is my first time trying to create a cakephp plugin
Thank you in advance!
You have to load the plugins your parent app in APP/Config/bootstrap.php
CakePlugin::loadAll();
You do not need AppModel or AppController in your plugin. Your plugin has an own AppController/-Model named PluginNameAppController/PluginNameAppModel.
You can call your plugin at http://host/plugin_name/controller/action/[...]. In your case http://host/bug_cake/issues/view/1 for instance.
But you can also use custom routes in your plugin with plenty of options.
Hope that answers your question—if not, comment.

import Only CakePHP Html Helpers in Custom MVC project

I have an unusual task at hand, I just need to import a specific portion of CakePHP framework in my custom MVC project, i.e HTML helpers.
Can anyone tell me what code libraries from CakePHP do I need to transfer into my project , to do this.
Please note I need only HTML helpers from the CakePHP framework.
Thanks.
Just go to the CakePHP Github repository Helper directory and download them: https://github.com/cakephp/cakephp/tree/master/lib/Cake/View/Helper

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

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