how to read cookie value in cakephp view file - cakephp

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.

Related

Undefined variable in view

I am having an issue setting variables in the controller and showing it at the view.
my codes are as follow:
In my view (pages/anything.ctp):
<?php echo $anything; ?>
In my controller (pagesController.php):
public function anything() {
$a = "asdasdas";
$this->set('anything', $a);
}
I am new to Cake, and I've done quite a number of search in google and stack. Still no luck.
I'd be grateful if anybody could help, or if anyone already asked this question before please provide a link that would be best.
First read the following article Controller actions in CakePHP CookBook
When you use controller methods with requestAction(), you will often want to return data that isn’t a string. If you have controller methods that are used for normal web requests + requestAction, you should check the request type before returning:
class RecipesController extends AppController {
public function popular() {
$popular = $this->Recipe->popular();
if (!empty($this->request->params['requested'])) {
return $popular;
}
$this->set('popular', $popular);
}
}
The above controller action is an example of how a method can be used with requestAction() and normal requests. Returning array data to a non-requestAction request will cause errors and should be avoided. See the section on requestAction() for more tips on using requestAction()
Try this:
public function anything() {
$a = "asdasdas";
$this->set(compact('a'));
}
<?php echo $a; ?>

cakephp disable direct access to a controller

I am new to use cakephp2,
i use element + requestAction to show a news block on some page of my site, like below:
news.ctp
<?php
$news = $this->requestAction('controller'=>'News','action'=>'load');
foreach($news as $itm){
echo $itm['title];
//...
}
NewsController.php
<?php
//...
public function load(){
//...
return $data;
}
It's worked well ,my problem is
how to disable direct access like: http://domain/News/load
and if it is a good way to make a contents block?
thanks!
In your controller you can try like this to prevent direct access.
public function load(){
if (empty($this->request->params['requested'])) {
$this->redirect($this->referer());
}
return $data;
}
If requestAction is used without caching requestAction can lead to poor performance. It is rarely appropriate to use in a controller or model. check here
For more info you can check the documentation here

cakePHP not able to use a global variable in my views

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');

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.

Accessing actions with prefixes in cakephp

im still noob with cakephp. I want to access an action with a prefix but im being redirected to my current view. how can i do this? Example:
ihave a function like below:
function admin_getID()
{
some codes here...
}
in my link. i accessed it using this html helper:
$this->Html->url(array('action'=>'getID', 'admin'=>true))
note that currently i dont have any prefix and i want to access the action with a prefix.
the url will be used in jQuery.ajax's URL so in the jquery,
jQuery.ajax({
...
url:"Html->url(array("action"=>"getID", "admin"=>true))?>",
...
});
Thank you!
Since in your file core.php you are using the same prefix, for example:
Configure::write('Routing.prefixes', array('admin'));
You should use:
echo $this->Html->link('link', array('action' => 'getID', 'admin' => true));
This will generate the link /admin/{your_controller}/getID.
For the same Controller, but if you want to display to another controller, you must include the controller parameter in the array.
If you're not using the directive Routing.prefixes as I said above, simply add the admin_getID parameter in action value.
I think you are talking about routing. So for example, if you want to define actions for admin like:
admin_index
admin_edit
admin_view
adn have them accessible by
example.com/admin/index
example.com/admin/edit
example.com/admin/view
This is called routing in CakePHP. You can see how this is done here:
http://book.cakephp.org/1.3/en/view/948/Defining-Routes
Update
You can just do this:
<?php echo $this->Html->link('link', array('controller' => '{CONTROLLER_NAME}', 'action' => 'getID', 'admin' => 'true')); ?>
UPDATE 2
You are not echoing your URL. You need to do this:
jQuery.ajax({ ... url:"<?php echo $this->Html->url(array("action"=>"getID", "admin"=>true)); ?>", ... });
If you are not using PHP to render your jQuery, you cannot use cake to generate your URL, you will have to do it manually:
jQuery.ajax({ ... url:"/admin/CONTROLLER/getID", ... });

Resources