cakePHP not able to use a global variable in my views - cakephp

I am using cakephp v2.3.4
Have set in the /app/Config/bootstrap.php a global variable as follow;
Configure::write('Bike.Frontwheel','Gazette, 16 inch');
I am not able to see this value in my any of my views, what am I doing wrong?
I am able to change the value in a controller with:
Configure::write('Bike.Frontwheel', $Data['Bike']['description']);
Code in the view:
<?php echo Configure::read('Bike.Frontwheel'); ?>

Set the variable in your controller so that it gets passed to your view:
$this->set('front_wheel', Configure::read('Bike.Frontwheel'));
Then in your view:
<?php echo $front_wheel; ?>

try in the config folder,
for this you need to create a file in the config folder & load that in the bootstrap as shown
Configure::load('custom');
this shows that your file is custom.php
now you can write as
$config['Bike.Frontwheel'] = 'Gazette, 16 inch';
where ever you need you can call this as
$variable = Configure::read('Bike.Frontwheel');

Related

controller logic for included view files in cakephp

As I am including elements/header.ctp file in another main.ctp file..
<?php echo $this->element('header'); ?>
I have included header in main.ctp it is displaying quite good. but when I am writing the query in ElementsController in is throwing an error undefined varible..
Below code for Elements Controller header() is the included file function.
public function header()
{
$this->set('marquee',$this->Newsmaster->find('all',array('order'=>array('Newsmaster.priority DESC'),'limit' =>20)));
$userid = $this->Session->read('user');
}
can anyone help me in solving this please... thank You
Elements Controller is not needed to set "marquee" varaible , you need to code in your usual controller for eg. for pages/index you will need to set variable in PagesController's index function not in under ElementsController

blackhole cakephp 2 associated entities

My Goal:
Reuse of a contact form to be related to several different entities I call "Parents" ie Group has contact information, Member has contact info etc....
The way I tried doing it was:
1. Creating a view file for contact, named "form.ctp" which doesn`t create a new form, nor submits, just echo's the contact's fields.
2. Calling this file using requestAction
My Problem:
The form's _Token get crumbled.
Parent add.ctp example
<?php echo $this->Form->create('Group');?>
<fieldset>
echo $this->Form->input($field_prefix.'contact_id',array('type'=>'hidden'));
<?php echo $this->requestAction(array('controller' => 'contacts', 'action' => 'form'), array('named' => array('index'=>'0','parent'=>'Group',
'fields'=>array(
'email'=>array('value'=>'xx#yy.com','hidden'=>1)
))));
inside the form.ctp I have:
//Associated Model
echo $this->Form->input('Contact.0.city',array('type'=>'hidden'));
echo $this->Form->input('Contact.0.postcode');
echo $this->Form->input('Contact.0.phone');
echo $this->Form->input('Contact.0.cellphone');
echo $this->Form->input('Contact.0.email',array('value'=>""));
echo $this->Form->input('Contact.0.id',array('type'=>'hidden'));
?>
Looking at the HTML source code that is generated, I see that whether I use the request action or just copy the contect of the form.ctp into the "Parent's" add file, I get the same HTML result.
HOWEVER!!! when I use the form.ctp Action Request, I get the blackhole, the tokens are being messed up!!!
Any Ideas?
Thanks in advance
Orly
If your problem is solely reusing a form, you can use the form as a Element, and then you could call it multiple times, substituting in the exact values you need.
As for SecurityComponent, I would recommend (at least as a temporary fix) disabling SecurityComponent for that specific action by using $this->Security->unlockedActions(); in your controller's beforeFilter()

Including JS in default View in Cake PHP

I can do this in any of my View file in Cake PHP:
<?php echo $this->Html->script('myjs.js', false); ?>
but if I do the same thing in my default view (default.ctp) then the JS files don't load i.e. they don't get echoed. I have tried moving includes above and below <?php echo $scripts_for_layout ?> in the default view but they still don't get printed:
<?php echo $scripts_for_layout ?>
<?php echo $this->Html->script('myjs.js'); ?>
<?php echo $this->Html->script('myjs.js'); ?>
<?php echo $scripts_for_layout ?>
Any ideas?
Thank you :).
with or without an extension - this is not the reason why the script do not get echoed,
as mensch mentioned before it makes no difference because of:
source of html helper cakephp 2
if (strpos($url, '?') === false && substr($url, -3) !== '.js') {
$url .= '.js';
}
the reason is:
you have already inserted this script inside your view,
cake checks if the script is already inserted on the page
options - 'once' - Whether or not the script should be checked for uniqueness. If true scripts will only be included once, use false to allow
the same script to be included more than once per request.
by default the value of once is set to TRUE
remove the script from your view first and then try it with or without '.js'
P.S.: why the petervaz answer has worked for you:
because this check:
if ($options['once'] && isset($this->__includedScripts[$url])) {
return null;
}
made before check for file extension
so isset(__includedScripts['myjs']) == false // because first key was __includedScripts['myjs.js']
and you've got your script included
I have a project with scripts added just before $scripts_for_layout and they are displayed fine. The only difference is that I'm not adding the extension .js to the call, like this:
default.ctp:
<head>
<?php echo $this->Html->charset(); ?>
<title>
<?php echo "Site > $title_for_layout"; ?>
</title>
<?php
echo $this->Html->script('jquery-1.6.4');
echo $this->Html->script('jquery-ui-1.8.16.custom.min');
echo $this->Html->script('mercado');
echo $scripts_for_layout;
?>
</head>
Another thing I'd DEFINITELY recommend is to check that loading your JS in a view is 100% OK with everything.
Do NOT use $this->Html->script if you are unsure about:
--- the dependency of scripts on each other
--- the dependency of some plugins on other scripts
--- how many different layouts and/or views you want to use in the future.
For example, some plugins use jQuery but they require jQuery to be loaded BEFORE the plugin itself.
So if you put jQuery in a view or layout, the plugin's JS loads before jQuery and plugin may not work. If you want to make sure some script loads first, you can try this:
in your app/View/Helper/AppHelper.php:
class AppHelper extends Helper {
public $helpers = array('Html', 'Session');
public function beforeRender($viewFile){
echo $this->Html->script('jquery', array('inline'=>false));
}
}
Therefore you don't have to include jQuery in multiple views and it loads first.
$scripts_for_layout in a layout is a placeholder for all the scripts included in views, so if you include it or don't won't have any effect if you load scripts directly in the default.ctp.
That being said, adding the extension or leaving it out of the $this->Html->script() call shouldn't make a difference. What could be the case is that the HtmlHelper isn't called correctly (it's called by default in Cake 2.0), but you should be receiving error messages. Is debug set to 2 in /app/Config/core.php?

how to read cookie value in cakephp view file

in this i write the cookie value in controller file.
i wanna read that cookie value in view file than how it possible.
You must read it in the controller and set the value to make it available to the view:
$this->set('myValue', $this->Cookie->read('cookieValue'));
Then in the view, you can access the variable $myValue to return the value of 'cookieValue':
<?php echo $myValue; ?>
After all CakePHP is a PHP framework, you can read them by $_COOKIE :)
Bear in mind that you should use:
$this->Cookie->write('myValue', $value, false);
in your controller, because otherwise it will be encrypted and it will be hard to use :)
Use Cookie components in AppController:
$components = array('Cookie');
Define following in AppController's beforeFilter():
$this->set('cookieHelper', $this->Cookie);
So that you could use it in view:
$cookieHelper->read('something');
I use the SessionComponent and SessionHelper to do this:
In the controller:
$this->Session->write('first_visit', true);
In the view:
if ($session->check('first_visit')) {
$session->del('first_visit');
echo $this->element('quick_intro');
}
You can also use $session->read('value') to read out a value from the session, instead of just checking if it exists.

how to store an array in the controller and use in the view file in cakephp

In my Users controller, I find the number of forms that the user has created by querying the 'Forms' table and store it in an array. But how to use the array variables in the view file.
Normally, I would set the variable in the controller using set function and use it in the view file as such. But how to set an array in the controller? I get the values in the controller(did an echo).
My controller code is:
function users(){
$this->set('users',$this->User->find('all'));
$users=$this->User->find('all');
foreach($users as $user):
$id=$user['User']['id'];
$userFormCount[$id]=$this->Form->find('count',array('conditions'=>array('Form.created_by'=>$user['User']['id'])));
$userFormCount[$id]=$this->Form->find('count',array('conditions'=>array('Form.created_by'=>$user['User']['id'])));
echo "users : ".$id." ".$userFormCount[$id];
endforeach;
}
My view file:
<?php foreach($users as $user):?>
<tr>
<td class="people_profile">
<?php echo $user['User']['name'];?>
</td>
<td>
<?php
$id=$user['User']['id'];
echo $userFormCount[$id]." ";?>forms, //need the array value here.
0 reports,
0 badges
</td>
</tr>
<?php endforeach;?>
Since I have stored the value in a varaible $userFormCount in the controller, I do not get the value in the view file. but how to set and get an array value?
First of all you should avoid repeating your function calls.
First two lines of your function can be replaced by:
$users=$this->User->find('all');
$this->set('users', $users);
Next you can alter your object before passing it to view and add the property you are calculating to that object.
function users(){
$users=$this->User->find('all');
// notice the & here
foreach($users as & $user):
// you are repeating the same line here as well, that's not necessary
$user['User']['custom_field'] = $this->Form->find('count',
array('conditions'=>array('Form.created_by'=>$user['User']['id'])));
endforeach;
$this->set('users', $users);
}
and you can then use it in your view like any other field that you get from the database. To improve it even firther you may want to consider moving this code to a afterFind callback in your User model, at least if this custom values are used more then once.
You already know how to set an array value :-)
In this code you are setting the array 'users'
$this->set('users',$this->User->find('all'));
So you can do the same for the other array:
$this->set('userFormCount',$userFormCount);
...and read it in the same way as your users variable.
But please look at RaYell's answer, because it explains how to make your current code better.

Resources