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');
Related
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
I am making one CakePHP project with Auth component. When I log in I got Session variable with user data. At the moment I am using this variable in controllers to pass data to the model.
$user = $this->Session->read('Auth');
$costs = $this->Posts->get_quartal_cost($user, $quartal, TRUE);
As I am using this in many controllers/models I am thinking that this is not DRY approach, so I wanted to make it better - something in AppModel(?)
Do you have some advice how to do that better?
Thanks
You could use the beforeFilter event in your AppController and do something like this:
public function beforeFilter()
{
if ( $this->Session->check('Auth') )
Configure::write('Auth', $this->Session->read('Auth'));
}
From anywhere in your controllers, models and even views, you'll be able to access it by using echo Configure::read('Auth');. See the Configuration class documentation for more information.
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.
How do I call a function from a component from a view? (ctp file)
Is that even a good practice to do that?
Thanks,
Tee
i suggest wrapping the component methods in a helper, and then using the usual route to access the helper.
For most components you could use something like:
App::import('Component', 'YourComponent');
$theComponent = new YourComponent();
$theComponent->yourMethod();
However, components are meant to share functionality used by controllers, so usually you should avoid calling components from views.
ALL IS POSSIBLE!
Component:
function initialize(&$controller){
$this->controller =& $controller;
$this->controller->set('YourComponent', new YourComponent());
}
View:
<?php $YourComponent->doAction() ... ?>
My opinion is it is not good practice to do that. Think of a view as something designers would work with. You want to keep your code in the models and controllers if at a possible. If it has to do with reusable view content, consider moving the controller to an element or a helper, as that is what they are intended for.
If you provide more details, we can be more specific to how to implement something if you already have a code base we can reference.
In cakephp 3
$config = new \Cake\Controller\ComponentRegistry();
$CustomComponent = new \App\Controller\Component\CustomComponent ($config);
In my cakephp application i need to use my controller in helper.php. Its not working. will any one explain it with little syntax?
I really hate thinking that code can tell me how to think instead of me telling it what to do ... anyway ... here is an example for loading the controller rendering the page, and it can load any controller inside the helper.
<?Php
class HelperNameHelper extends AppHelper{
private $controller;
public function __construct(View $view, $settings = array()) {
parent::__construct($view, $settings);
$this->controller=$this->loadController();
}
protected function loadController($name=null){
if (is_null($name)) $name=$this->params['controller'];
$className = ucfirst($name) . 'Controller';
list($plugin, $className) = pluginSplit($className, true);
App::import('Controller', $name);
$cont = new $className;
$cont->constructClasses();
$cont->request=$this->request;
return $cont;
}
}
EDIT: just realiced this is such an old post :( ... this is working in cakephp 2.2.3
Helpers are not designed to have access to the controller. If you're trying to access the controller you may want to rethink your application design.
I actually had this same problem myself. I have a CakePHP 1.1 application where I was accessing the controller by passing the name of the controller as a parameter to the view. After trying to upgrade the code to work with 1.2 I realized that this was bad design. It was my first experience with CakePHP and MVC, so I chalked that up as a lesson learned.
MVC requires some up-front design to make sure that you're putting your functions in the right places (controller, model, or view).
(P.S. You can also try the #cakephp channel on irc.freenode.net)
Its an MVC.
You shouldn't use a Controller in your helper, rather use your helper in your controller.
Why would wan't to do that in the first place.
Read this:
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller