I have custom utility class that contains some methods for general use and it is located in src/Utility/UtilityClass.
I can include the Utility class in controller .e.g.
use App\Utility\ArrayUtil;
and then call the class in my controllers e.g.
ArrayUtil::myMethod();
And it works. I need to include the UtilityClass in bootstrap so it applies all over the application so I can reuse it in models, templates and other controllers as well.
I tried to load it in config/bootstrap.php but I get this error:
Error: Class 'ArrayUtil' not found
Any idea
You can add this line at the top of the page, be it Model, view or controller.
use App\Utility\ArrayUtil;
If you're using this utility in multiple views, then I'd suggest you to write this line in Template/Layout/default.ctp, since all templates would be a part of this.
Hope this helps.
Peace! xD
Related
i have a controller PostmustController which doesn't have any models naming Postmust. it will load a model named Post how can i do it in cakePHP.
Learned and solved it. And there is a anotherway.
You can also use this
Controller::loadModel('Post');
You can learn the detail from the following link.
Uses following array in your controller it will automatically load models in your controller.
public $uses = array('Post');
I'm a newbie in CakePHP, please have patience with me :)
So, I'm trying to create a plugin called References. I've baked "plugin's core" through cake's console. Then I've created ReferencesController class that extends ReferencesAppController and Reference class (model) that extends ReferencesAppModel. My next step was creating action admin_index (just code to save form), it's view and a little bit validation in Reference model. To my problem, I'm unable to create any other action, ex. admin_add. When I do (I create new action and I add it's view), then I try to access it through the URL (localhost/my_project/admin/references/add), and there comes the message "Error: References.AddController could not be found.". I am not sure, what I do wrong, I don't want to create another controller, just action. Thank you
Because only the plugin index action (when plugin and controller have the same name) is directly routed.
For all others you need to verbosly add the plugin name and the controller name to the url:
/my_project/admin/references/references/add
If you had created a link to this action, the routing would have shown you that.
I'm using cakephp and I have set a simple site, when rendering index() it works fine
but when programming other methods of the same controller, the views for them do not show the
background, it's like it cannot find the images, I thought the layout would be preserved for all views.
Your View/Layouts/default.ctp layout file will always be used unless specified otherwise. If it's not showing a background, it's like you're using an incorrect path for the image, css...etc.
If you want to apply a layout to all methods of a particular controller (but not all other controllers) then use a theme.
Controller Code:
class MyThingController extends AppController {
public $theme = 'MyTheme';
....
}
Next you have to put your layout file in:
/app/View/Themed/MyTheme/Layouts/default.ctp
Then all methods in your controller will use this layout by default.
See here for more info: enter link description here
(note: this answer applies to Cake version 2.1+)
I'm writing a poll plugin for a website based on CakePHP. The plugin works good if I access it from its own URL (eg. myapp.com/plugin/controller) but I need to call it from different pages. I would like to include it as a widget in every page.
I'm looking for a method like $myplugin->renderPoll($pollId); but I really didn't find any information about how to instantiate the Polls class. I tried with App::import and ClassRegistry::init with no luck.
Any suggestion ?
Thank you
Looks like you are trying to create some sort of Helper to create poll cross views? I would suggest creating a Helper for that particular class. Simply create a helper in plugins/plugin_name/views/helpers/foo.php, and in each controller (or in app_controller.php) that you need it, include the helpers as $helpers = array("PluginName.Foo"); and inside your view, you should be able to use the methods defined in foo.php by calling $foo->renderPoll($pollId).
//app/plugins/plugin_name/views/helpers/foo.php
class FooHelper extends AppHelper {
var $name = "Foo";
function renderPoll($id=0) {
//...
}
}
Use Elements! They're small blocks of presentation code that need to be repeated from page to page, sometimes in different places in the layout.
Check this link out: http://book.cakephp.org/view/1081/Elements
I guess this link explains everything you need.
I have made a helper class called Navigation that gets used on every page because it does stuff to my main navigation menu. So in order for this to work I have included the helper in my pages controller like so:
var $helpers = array('Html', 'Javascript', 'Navigation');
however when there is an error like a missing view or something the helper can't be found and I get a reference to a non-object error that messes my page layout up. I'm guessing this is becuase an error page uses a different controller, however there isn't a file error_controller.php or anything in the controllers file. So my question is where do I need to declare the helper so it can be found by an error page. Would I need to make an error controller file or is there already a file that I can add it in to?
Any help would be much appreciated
Thanks
If it's used on every page, why not add it to the AppController?