I have a little trouble with naming convention in CakePHP 1.3. I've element called storeItem.ctp, when I'm trying to call it from another view - it works perfect, but when I'm trying to use it as view for action($this->viewPath = 'elements'; and then $this->render(null, 'ajax', '/canvas/storeItem');) I'm getting the error: Error: Confirm you have created the file: ***views/elements/canvas/store_item.ctp.
How can I fix it without renaming element?
An explanation why your code does not work as expected is written in the second paragraph from the bottom in the cookbook. You should try it this way:
$this->layout = 'ajax';
$this->render('/elements/canvas/storeItem');
Related
I'm attempting to create a custom display in yii2 framework using this code in my site controller:
/******/
public function actionChartDisplay()
{
return $this->render('chartDisplay');
}
for testing purposes I pasted the form name in my actionAbout function as a parameter to the render function in it. It worked with this:
public function actionAbout()
{
return $this->render('chartDisplay');
}
But I need to create many custom views in yii2 and this won't be a solution.
This is the error I get
I'm curious as to why it is. Since I was following this tutorial and came across this weird behaviour.
My 'chartDisplay.php' file is merely a "hello world" that does work with the action about function.
in yii2, the controllers and actions with multiple words, that are marked by capital letters are divided by - in your request, so in your case the route would be some/chart-display
Apparently as #SmartCoder pointed out it was an error on how Yii2 Handles the action functions in its controller however I didn't mark his answer as the solution right away because implementing it resulted in an error. So aside from that I'm posting the way I solved it.
So instead of using chart-display I simply changed it for "charts" like this:
public function actionCharts(){
return $this->render('charts');
}
Changed the name of my file so it fits to charts.php and it worked.
I have a controller name CodeBridgeController and have a model table name CodeBridgeTable.
When I call CodeBridge table in action like following:
$this->CodeBridge->find('all')->toArray();
This generate Error: Call to a member function find() on a non-object
File D:\php\htdocs\cake3\erp\src\Controller\CodeBridgeController.php
Line: 25
I think this is silly matter but i could not find any wrong in the code why table is not loaded in this context.
When i call this way it works.
$codeBridgeTable = TableRegistry::get('CodeBridge');
$results = $codeBridgeTable->find('all')->hydrate(false)->toArray();
What may create the problem, expert suggestion needed.
Finally, I found where the problem lies.
In controller action, I generating a url like following:
Router::url(array('controller' =>'codebridge','action'=>'edit')); which generate url /cake3/erp/codebridge/edit
After changing the controller name in camel case Router::url(array('controller' =>'CodeBridge','action'=>'edit')); which generate url '/cake3/erp/code-bridge/edit`
This solve the problem
To resolve this, just load the model prior to trying to use it:
$this->loadModel("CodeBridge");
$this->CodeBridge->find('all')->toArray();
In CakePHP, I am trying to create a autocomplete function from a textbox, using this tutorial: http://bakery.cakephp.org/articles/matt_1/2011/08/07/yet_another_jquery_autocomplete_helper_2
I keep getting a
Error: AutoCompleteHelper could not be found.
Undefined index: autoCompleteText
I am unsure how to fix this as I have tried relocating the files and still the same error.
I create the below files and copy the code from the tutorial,
create the auto_complete.php in the view/helpers directory,
and create webroot/js/views/helpers/auto_complete.js.
controller - Tests
function auto_complete
class TestsController extends AppController {
public $helpers = array('Html', 'Form', 'Session','Js','AutoComplete');
public function auto_complete() {
$this->loadModel('Tutor');
debug( $this->params);
$terms = $this->Tutor->find('all', array(
'conditions' => array(
'Tutor.first_name LIKE' => $this->params['url']['autoCompleteText'].'%'
),
'fields' => array('Tutor.first_name'),
'limit' => 3,
'recursive'=>-1,
));
$terms = Set::Extract($terms,'{n}.Tutor.first_name');
$this->set('terms', $terms);
$this->layout = '';
}
view
auto_complete.ctp
<?php
echo $this->AutoComplete->input(
'Tutor.first_name',
array(
'autoCompleteUrl'=>$this->Html->url(
array(
'controller'=>'tests',
'action'=>'auto_complete',
)
),
'autoCompleteRequestItem'=>'autoCompleteText',
)
);
if(isset($terms)) {
echo $this->Js->object($terms);
}
echo $this->Form->create('');
echo $this->Form->input('type', array('label' => 'Choose' ));
echo $this->Form->end('send');
?>
I had a good look around previous posts and I really couldnt solve the problem.
Cakephp form input with autocomplete
The tutorial you're looking at is for CakePHP 1.x (it's from August 2011, Cake 2 came out in October '11), but you're running Cake 2.5. The naming standards have changed between 1.x and 2.x, so Cake can't see your helper.
First: Your main problem is that you're naming your helper auto_complete.php when it should be AutoCompleteHelper.php.
Second, you are placing it in the wrong directory. In Cake 1.x, helpers lived under /view/helpers/. In 2.x, they live under /View/Helper/.
Third, regarding the undefined index error, you need to understand how this helper works in the first place, and why you're implementing it incorrectly.
The controller action auto_complete should not have a view. It's just a data source. When you use the helper, it loads the Javascript, which does its magic by polling /auto_complete?autoCompleteText=stringgoeshere.
You might notice that you removed $this->layout = 'ajax'; from the controller action, probably because your toolbars weren't rendering. They weren't supposed to. It's just a datasource, it NEEDS the ajax layout, and there should be no view on that action.
Because you were trying to put a form on your auto_complete action and not on another action, the helper was looking for the query parameter autoCompleteText, not finding it, and displaying an error.
You can solve this by changing the auto_complete action back as close as possible to how it was in the example, deleting auto_complete.ctp, and by trying to use the helper in the correct view, like add.ctp.
Fourth: You shouldn't be using jsHelper anymore. It's been deprecated in 2.5 and is going to disappear in the future. Basically, the tutorial is obsolete.
However, it's really easy to just replace $this->set('terms', $terms); with return json_encode($terms) and write your own piece of JS to make an AJAX call. I suggest incorporating Typeahead.js.
Finally: You should consult the 1.x -> 2.x migration guide, and as well, once you get your helper running you've got some other typos in your code that will break it anyway- "Ttuor.first_name", for example. Good luck!
I'm working on a project that allows external users(coming from another source than just my server) to make a GET request to a page on my server, which will then return some JSON encoded data.
For example, say the data (not using Cake, just standard PHP) would be sent to
wwww.example.com/handlerequest.php
I'd just have something like
if(isset($_GET['userRequest'])){
//do some stuff
echo $json_encoded_stuff;
}
With CakePHP I'd just post the data to something like
www.example.com/HandleRequest
However, I do not want/need a view for this because there is nothing to see. This page is purely for data exchange. Considering this, is there anything special I have to do so that Cake doesn't throw an error because it's expecting a corresponding view? Is this even possible?
It is easy to disable both the layout and view in CakePHP by putting the following line in your controller action:
$this->autoRender = false;
If you want to disable just the layout, use the following line in your controller action:
$this->layout = false;
And if you only want to disable the view for this action, use the following line in your controller:
$this->render(false);
Note that using $this->layout = false; and $this->render(false); together in your controller action will give you the same results as $this->autoRender = false;
In the Agile Toolkit Tutorial (Jobeet), I setup the quick data model (Page 3) and it looks nice with the CRUD test page. I tried to change a line of code in the test.php file. The problem is when I added the paginator, line of code, the data in the grid disappeared. Is this a limitation to the paginator class? Is there a quick way to get this custom grid paginated? Thanks.
Original Code displays method not defined error when adding addPaginator as shown below:
$this->add('CRUD')->setModel('Category');
//$this->add('CRUD')->setModel('Job');
$jobCRUD=$this->add('CRUD');
$jobCRUD->setModel('Job');
$jobCRUD->addPaginator(3); //This line causes an method not defined error
Modified code using setSource doesn't display an error but displays an empty grid:
class page_test extends Page {
function init(){
parent::init();
//$this->add('CRUD')->setModel('Category'); //Not needed for my example
$grid=$this->add('Grid');
//$grid->setModel('Job'); //Removed this to show custom columns
$grid->addColumn('id');
$grid->addColumn('type');
$grid->addColumn('position');
$grid->setSource('job');
$grid->addPaginator(3); //Added this to paginate the results (doesn't work & removes data)
}
}
Solution:
$this->add('CRUD')->setModel('Category');
//$this->add('CRUD')->setModel('Job');
$jobCRUD=$this->add('CRUD');
$jobCRUD->setModel('Job');
$jobCRUD->grid->addPaginator(3); // This fixed the paginator
Jobeet is for Agile Toolkit 4.1 and uses "setSource" which is obsolete.
I think if you replace setSource with setModel it should be OK.