App::import not working for controller - cakephp

I need to load controller class and I try:
App::import('Controller','AppController');
or
App::import('Controller','PagesController');
But this every time returns false. When I try this:
App::import('Model','AppModel');
it returns true, so it seems it's not working for controllers - why?

When using App::import(type, name) for a controller, you don't need to include "Controller" in the name you import, just when creating the instance variable.
App::import('Controller', 'Pages');
$Pages = new PagesController;

Related

grails: show list of elements from database in gsp

in my grails app I need to get some data from database and show it in a gsp page.
I know that I need to get data from controller, for example
List<Event> todayEvents = Event.findAllByStartTime(today)
gets all Event with date today
Now, how can I render it in a gsp page?How can I pass that list of Event objects to gsp?
Thanks a lot
You can learn many of the basic concepts using Grails scaffolding. Create a new project with a domain and issue command generate-all com.sample.MyDomain it will generate you a controller and a view.
To answer your question create a action in a controller like this:
class EventController {
//Helpful when controller actions are exposed as REST service.
static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
def showEvents() {
List<Event> todayEvents = Event.findAllByStartTime(today)
[eventsList:todayEvents]
}
}
On your GSP you can loop through the list and print them as you wish
<g:each in="${eventsList}" var="p">
<li>${p}</li>
</g:each>
Good luck
I am not sure if this is really what you meant, because in that case I suggest you to read some more on the grails :), but anyway, for your case you can use render, redirect as well but here I am taking simplest way:
In your controller you have:
def getAllElements(){
List<Event> todayEvents = Event.findAllByStartTime(today)
[todayEvents :todayEvents ]
}
and then in the GSP(I assume you know about grails conventions, as if you don't specify view name, it will by default render gsp page with the same name as the function in the controller, inside views/):
<g:each in="${todayEvents}" var="eventInstance">
${eventInstance.<propertyName>}
</g:each>
something like this.

CakePHP - Class ** cannot be found

I have a strange problem with CakePHP
CakePHP gives an error on the following line:
View/Designer/cards:
$this->JsBridge->set('Card.DISPLAY_TYPE_FOLDER_GREETING', Card::DISPLAY_TYPE_FOLDER_GREETING);
Class 'card' can not be found.
However in DesignersController I load the model Card via de following line:
public $uses = array('Designer', 'Card');
If I add the following line in the top of DesignersController
App::uses('Card', 'Model');
The page loads, but the following line does not work:
$this->paginate = $this->Card->getPagination($filter);
I have put the code for the model Card.php here : http://pastebin.com/U7zxKHCx
Can you tell me what is going wrong?
Thank you!
Are you including the CardModel in your Controller?
$uses = array('....','Card',....);
Controller properties, classes attached etc. are NOT directly accessible in a View. You will need to set this constant Card::DISPLAY_TYPE_FOLDER_GREETING to a variable:
$this->set('variableName1', Card::DISPLAY_TYPE_FOLDER_GREETING);
Then use it in the View:
$this->JsBridge->set('Card.DISPLAY_TYPE_FOLDER_GREETING', $variableName1);

How to use find('all') in Views - CakePHP

I searched a lot but I couldn't find on How to use the find('all') in Views as used in Rails, but here I'm getting the error "Undefined property: View::$Menu [APP\Lib\Cake\View\View.php, line 804]"
'Menu' is the model which I'm using to fetch data from the menus table.
I'm using the below code in views:
$this->set('test',$this->Menu->find('all'));
print_r($test);
Inside your Menu model create a method, something like getMenu(). In this method do your find() and get the results you want. Modify the results as you need and like to within the getMenu() method and return the data.
If you need that menu on every page in AppController::beforeFilter() or beforeRender() simply do
$this->set('menu', ClassRegistry::init('Menu')->getMenu());
If you do not need it everywhere you might go better with using requestAction getting the data using this method from the Menus controller that will call getMenu() from the model and return the data. Setting it where you need it would be still better, if you use requestAction you also want to cache it very likely.
TRY TO NOT RETRIEVE DATA WITHIN VIEW FILE. VIOLATION OF MVC RULE
try this in view file:
$menu = ClassRegistry::init('Menu');
pr($menu->find('all'));
In AppHelper ,
Make a below function
function getMenu()
{
App::import('Model', 'Menu');
$this->Menu= &new Menu();
$test = array();
$test = $this->Menu->find('all');
return $test;
}
Use above function in view like :
<?php
$menu = $html->getMenu();
print_r($menu);
?>
Cakephp not allow this .
First create the reference(object) of your model using ClassRegistry::init('Model');
And then call find function from using object
$obj = ClassRegistry::init('Menu');
$test = $obj->find('all');
echo ""; print_r($test); `
This will work.

Passing data from CakePHP component to a helper

I need to share data between a component and helper. I'm converting my self-made payment service formdata generator to a CakePHP plugin and I'd like to be able to fill in the payment data from the controller(using a component) and use a helper to print out the data.
Everything I've tried so far have felt a little too hacky, so let me ask you: Is there any elegant way to pass data from a component to a helper?
edit:
I solved this particular situation by adding the original formadata class instance to ClassRegistry during the component initialization. This way the helper too can access the instance using ClassRegistry.
However, this only works for objects, so the question remains open.
Having a similar problem, I found this solution to work best for me.
You could use the helper's __construct method in pair with $controller->helpers array.
Since the Helper::_construct() is called after the Component::beforeRender, you can modify the $controller->helpers['YourHelperName'] array to pass the data to your helper.
Component code:
<?php
public function beforeRender($controller){
$controller->helpers['YourHelperName']['data'] = array('A'=>1, 'B'=>2);
}
?>
Helper code:
<?php
function __construct($View, $settings){
debug($settings);
/* outputs:
array(
'data' => array(
'A' => (int) 1,
'B' => (int) 2
)
)
*/
}
?>
I am using CakePHP 2.0, so this solution should be tested for earlier versions.
Is there any elegant way to pass data from a component to a helper?
Yes, the same way you pass any data to the helper. In your view.
Inside your component I would do something like the following. The beforeRender() action is a CakePHP component callback.
public function beforeRender(Controller $controller) {
$yourVars = 'some data';
$goHere = 'other stuff';
$controller->set(compact('yourVars', 'goHere'));
}
Then in your view you can pass the data off to your helpers just like normal.
// view or layout *.ctp file
$this->YourHelper->yourMethod($yourVars);
$this->YourHelper->otherMethod($goHere);
In addition to what #Vanja, you can also do this just prior to instantiating a new view in your controller:
// In your controller method
// must be set prior to instantiating view
$this->helpers['YourHelperName']['paramsOrAnyName'] = ['var' => $passed_var];
$_newView = new View($this);
$return_result = $_newView->render($element_to_view, $layout);

send parameter url

I am sending the values to a controller doing this
echo $html->link('Do this? ',"/item/view/{$form->value('table.id')}");
on the controller end cake just pickups the id like this
function view($id = null){
....
}
Now in addition to table.id, I want to send another value called table2.source ...how would I do that so the controller also gets it
function view($id = null, $source=null) ...something along those lines but not sure how will they get to the controller in first place
Can you not just change the link url to something like this:
echo $html->link('Do this? ',
"/item/view/{$form->value('table.id')}/{$form->value('table2.source')}");
(After changing the view function to accept the second parameter...)

Resources