url passing parameters are not displayed - cakephp

In cake php layout/default.ctp
I created a menu bar but here I done link the URL with like this
<li class='active'> <?php //echo $this->Html->link('Psychiatrist', array('controller' => 'pages','action' => 'home','?' => array('id' => '1')));
echo $this->Html->link('Psychiatrist', array('controller' => 'pages', 'action' => 'home',5)); ?></li>
<!-- <li class='active'><a href='#'><span>Podiatrist</span></a></li>-->
<li class='active'> <?php echo $this->Html->link('Podiatrist', array('controller' => 'pages', 'action' => 'home',5)); ?></li></li>
It works fine, when we clicked on link url shows as
http:/Projects/DoctorSample/pages/home/5
controller /PagesController.php
class PagesController extends AppController {
public function home($id='null'){
if(isset($id)){
echo $id;
}
}
}
But in my display it shows
Error: The view for PagesController::display() was not found. but I done home.ctp in pages

The problem on your app/Config/routes.php file.
Delete this line from app/Config/routes.php -
Router::connect(
’/pages/*
’,
array(’controller’ => ’pages’, ’action’ => ’display’)
);

Related

SELECT in cake php

In this form I can't see name of select, this dropdown don't work, my code is:
In the controller I have:
public function admin_cambiar_centro() {
$this->Session->write(
'Auth.User.centro_id',
$this->request->data('CambioCentro.centro_id')
);
$this->redirect($this->referer());
}
In the view:
<?= $this->Form->create('CambioCentro', array(
'url' => array(
'controller'=> 'Users',
'action' => 'cambiar_centro'
)
)) ?>
<?= $this->Form->select('centro_id',
Hash::combine(AuthComponent::User('Centro'), '{n}.id', '{n}.nombre'),
array(
'empty' => false,
'value' => AuthComponent::User('centro_id'),
'style' => 'margin-top: 7px;',
'onchange' => 'this.form.submit()',
)); ?>
<?=$this->Form->end()?>
and this does not deploy I would be very grateful if you could help me
It looks like your form url is not correct. Try to add admin => true to the form's url.
<?=$this->Form->create('CambioCentro', array('url' =>
array('controller'
=> 'Users', 'action' => 'cambiar_centro', 'admin' => true)))?>
Another potential problem is that you are writing to the session and redirecting on every request, so you may create a redirect loop, and also on get request the session variable gets overwritten.
Check the request type in the action:
public function admin_cambiar_centro() {
if($this->request->is('post')) {
$this->Session->write('Auth.User.centro_id',
$this->request->data('CambioCentro.centro_id'));
return $this->redirect($this->referer());
}
}

How do I set a variable in a CakePHP model and access it in a view?

I have multiple models that I want to add the variable $section to, and then use that value in sidenav.ctp to dynamically change the sidebar. For example, my models are:
class Resource extends AppModel {
public $section = 'section1';
public $displayField = 'name';
public $order = 'modified DESC';
//validation, relationships, etc.
}
then I have another model like:
class Topic extends AppModel {
public $section = 'section2';
public $tablePrefix = '';
//validation, relationships, etc.
}
so in sidenav.ctp I want to do something like:
<?php if ($this->section == 'section1') { ?>
<li><?php echo $this->Html->link(__('Resources'), array('controller' => 'resources', 'action' => 'index')); ?></li>
<li><?php echo $this->Html->link(__('Topics'), array('controller' => 'topics', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('Log Out'), array('controller' => 'users', 'action' => 'logout')); ?> </li>
<?php } ?>
<?php if ($this->section == 'section2') { ?>
<li><?php echo $this->Html->link(__('Resources1'), array('controller' => 'resources', 'action' => 'index')); ?></li>
<li><?php echo $this->Html->link(__('Topics1'), array('controller' => 'topics', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('Log Out1'), array('controller' => 'users', 'action' => 'logout')); ?> </li>
<?php } ?>
but accessing $section this way doesn't work. I can't figure out how to set the value in the Model and then access it in the View. I'm aware that I could set the value in the Controller and then access it just by $section, but that would require me putting it in every function.
Try in your view file $this->name == 'ModelName'
like this example:
<li <?php if($this->name == 'Users'){?> class="active" <?php } ?> >
<a href="<?php echo $this -> Html -> url(array('plugin' => false, 'controller' => 'pages', 'action' => 'dashboard')); ?>">
<i class="fa fa-dashboard"></i>
<span><?php echo __('Dashboard', true); ?></span>
<span class="label label-warning pull-right">1</span>
</a>
</li>
OR
in your controllers beforeFilter or beforeRender methods:
//access variable from model
$this->set('sections',$this->User->section);
//or set direct
$this->set('sections','section1');
View's / Elements can be called and rendered by any controller, so they won't automatically know what data you want it to refer to unless you pass it from the model to the view.
The nearest you can do which isn't of any use would be to echo the Router model methods, but the results are dependent on the url you are at.
you can save define global variables
Configure::write('my_var','this is model variable');
and access this variable in your view file like this
echo Configure::read('my_var');

How to pass pagination parameters with additional params cakephp?

I have a list of categories which displays the list of items under it.
Thus:
routes have:
Router::connect('/category/:slug', array('controller' => 'Product',
'action'=>'catview', 'slug'=> '[0-9a-zA-Z]+'));
Router::connect('/category/:slug/:page', array('controller' => 'Product',
'action'=>'catview','slug'=> '[0-9a-zA-Z]+','page'=>'[0-9]+'));
and
when I do this in results page, it just doesn't work:
<?php
$slug = $this->params['slug'];
$this->Paginator->options(array('url'=> array('controller' => 'Product',
'action'=>'catview','slug'=> $slug)));
echo $this->Paginator->prev('<< Show me previous', array('class'=>'prev'))
. $this->Paginator->next('Show me more >>', array('class'=>'next')); ?>
It does not change the results, it shows the same result as it does on page 1.
Any ideas where I am going wrong?
Thanks to AD7six for the reference.
My controller needs to have :
$this->paginate = array(
//other stuff here
'paramType' => 'querystring'
);
Followed by:
$this->Paginator->options(array('url'=> array('controller' => 'Product',
'action'=>'catview','slug'=> $slug),
'convertKeys' => array('page')));
in the view file

Image link with $confirmMessage alert in Cakephp HTMLhelper - possible?

Is it possible to create an image with a link that also has a pop up alert [$confirmMessage] using the html helper in CakePHP?
This is my current text link:
$this->Html->link('Clear list', array('controller' => 'items', 'action' => 'clearlist', $model['Model']['id']), array(), 'Clear list?')
This how the image helper creates images with links:
echo $this->Html->image("recipes/6.jpg", array( "alt" => "Brownies", 'url' => array('controller' => 'recipes', 'action' => 'view', 6)));
However this allows only an htmlattributes array as the arguements for the link.
The $confirmMessage alert is not an html attribute is it?
This is the code I tried:
echo $this->Html->link($this->Html->image("clearall.png", array("alt" => "Clear list")), array('controller' => 'items', 'action' => 'clearlist', $model['Model']['id']), array(), 'Clear list?');
However this code printed the correct html for my img as text:
<img src="/img/clearall.png" alt="Clear list" />
Do I have to give up on htmlhelper in this case?
CakePHP does do this with the Html helper and you were really close!
<?php echo $this->Html->link($this->Html->image('clearall.png', array(
'alt' => 'Clear list')
), array(
'controller' => 'items',
'action' => 'clearlist',
$model['Model']['id']
), array(
'escape' => false,
'confirm' => 'Clear list?'
)); ?>
You could also have done it without the helper like so:
<a href="/items/clearlist/<?php echo $model['Model']['id']; ?>"
onclick="return confirm('Clear list?');">
<img src="/img/clearall.png" alt="Clear list" />
</a>
Thanks to ADmad and rtconner for showing me this in IRC.

How to call CSS class on a CakePHP Html->link?

I am a complete noob to programming and CakePHP all together, so please be patient. How am I supposed to call the CSS on this Html->link:
<?php echo $this->Html->link(__('Blogs', true), array('controller' => 'posts', 'action' => 'index') ); ?>
Please help.
Not sure what you mean by "call the css", I think you want to add a class to this link? IF that's the case you can just add another array as an argument and it will turn key =? value into HTML attributes. EG:
echo $this->Html->link(__('Blogs', true), array('controller' => 'posts', 'action' => 'index'), array('class' => 'my-class'));
This is all explained in the CakePHP docs.
Another Solution will be the inline style :
echo $this->Html->link(__('Blogs', true), array('controller' => 'posts', 'action' => 'index'), array('style' => 'color:red;font-size:20px;'));
Please try this CakePHP 3 in
<?php
echo $this->Html->link(__('Blogs'), ['controller' => 'posts', 'action' => 'index'], ['class' => 'mb-xs mt-xs mr-xs btn btn-warning']);
?>
I think this works:
<?php echo $this->Html->link('Add Task', array('action'=>'add'), array('class' => 'tac')); ?>

Resources