Hi all i have a menu like this on a cakephp 2 website:
<ul class="nav">
<li><?php echo $this->Html->link('Home', array('controller' => 'posts', 'action' => 'index')); ?></li>
<li><?php echo $this->Html->link('Add post', array('controller' => 'posts', 'action' => 'add')); ?></li>
<li>Contact</li>
</ul>
and i have to check if i'm on a page to add class="selected" on the menu link.
How can i do this?
Thanks
In your view file you can also do:
$this->request->params
I recommend you to write your own helper that will implement a method with the same args as HtmlHelper::link and internally call and return HtmlHelper but before it will compare $this->request->params with the passed array of the first arg. If it matches you can inject the class name in the 3rd arg.
Something like this, implement it on your own:
class MyHelper extends AppHelper {
public $helpers = array('Html');
public function link($title, $url, $options) {
/**
* if ($this->View->request->params ...
* do your matching logic here
* and if it matches: $options['class'] = 'active';
*/
return $this->Html->link($title, $url, $options
}
I wrote a (CakePHP 1.2) helper a while back that does this automatically:
http://richardathome.com/blog/cakephp-smarter-links
Should be pretty straight forward to port it to 2.0
Related
I use this partial to generate my submenu.
<?php foreach ($this->container as $page): ?>
<?php foreach ($page->getPages() as $child): ?>
<a href="<?php echo $child->getHref(); ?>" class="list-group-item">
<?php echo $this->translate($child->getLabel()); ?>
</a>
<?php endforeach; ?>
<?php endforeach; ?>
Which is called like this:
$this->navigation('navigation')->menu()->setPartial('partial/submenu')->render();
But when i render the menu the "$child->getHref()" renders the url without the needed "slug/id" parameter.
I tried to create the url with "$this->url()" in ZF1 you could pass the params in an array to the partial but in ZF2 that doesn't seem to work anymore.
Can anybody tell me how to add the params to the menu urls?
Thanks in advance!
PS!
I'm not referring to $this->Partial, i'm talking about $this->navigation('navigation')->menu()->setPartial('partial/submenu')->render() which apparently doesn't support a param array.
If I'm understanding your question, yes, you can pass params to partials. Example:
<?php echo $this->partial('partial.phtml', array(
'from' => 'Team Framework',
'subject' => 'view partials')); ?>
See http://framework.zend.com/manual/2.3/en/modules/zend.view.helpers.partial.html
I'm not sure this completely solves your issue, since you are not showing what the menu helper is. Is it your own view helper? Are you saying that setPartial method only accepts one argument?
All that said, have you considered Spiffy Navigation?
https://github.com/spiffyjr/spiffy-navigation
It's been sometime since this question was asked, however today I came across the same problem (using version 2.4).
If you have a segment route to be included within the menu that requires some parameters there is no way to pass these through to the navigation's view partial helper.
The change I've made allows a ViewModel instance to be passed to the menu navigation helper's setPartial() method. This view model will be the context for the navigation's partial template rendering; therefore we can use it to set the variables we need for the route creation and fetch them just like within other views using $this->variableName.
The change requires you to extend the Menu helper (or which ever navigation helper requires it).
namespace Foo\Navigation;
use Zend\Navigation\AbstractContainer;
use Zend\View\Model\ViewModel;
class Menu extends \Zend\View\Helper\Navigation\Menu
{
public function renderPartial($container = null, $partial = null)
{
if (null == $container) {
$container = $this->getContainer();
}
if ($container && $partial instanceof ViewModel) {
$partial->setVariable('container', $container);
}
return parent::renderPartial($container, $partial);
}
public function setPartial($partial)
{
if ($partial instanceof ViewModel) {
$this->partial = $partial;
} else {
parent::setPartial($partial);
}
return $this;
}
}
Because this extends the default implementation of the helper updated configuration is required in module.config.php to ensure the extend class is loaded.
'navigation_helpers' => [
'invokables' => [
'Menu' => 'Foo\Navigation\Menu',
],
],
The menu helper will then accept a view model instance.
$viewModel = new \Zend\View\Model\ViewModel;
$viewModel->setTemplate('path/to/partial/template')
->setVariable('id', $foo->getId());
echo $this->navigation()
->menu()
->setPartial($viewModel)
->render();
The only change in the actual partial script will require you to create the URL's using the URL view helper.
foreach ($container as $page) {
//...
$href = $this->url($page->getRoute(), ['id' => $this->id]);
//...
}
I am trying to insert record using Cakephp.My model name is something like User.php.
And My working controller name is SignupsController.I want to insert record using this two but I cant.I am give my some codes below :
View :
<?php echo $this->Form->create('Signups',array('action' => 'registration'));?>
<div class="row-fluid">
<div class="span5">
<label class="">*First Name</label>
<?php echo $this->Form->input('first_name', array('type' => 'text','label' => false, 'class' => 'input-xlarge validate[required]', 'div' => false)); ?>
</div>
<div class="span5">
<label class="">*Last Name</label>
<?php echo $this->Form->input('last_name', array('type' => 'text', 'label' => false, 'class' => 'input-xlarge validate[required]', 'div' => false)); ?>
</div>
</div>
<?php echo $this->Form->end(); ?>
My controller code is given below :
class SignupsController extends AppController {
var $name = 'Signups';
var $uses=array("User");
public function registration()
{
$this->layout="reserved";
$this->Club->create();
if (isset($_POST['registration'])) {
echo "This is";
echo "<pre>";print_r($this->request->data);echo"</pre>";
$this->User->save($this->request->data);
//$this->Session->setFlash(__('Promoter registration has been done successfully'));
//$this->redirect('registration');
//$this->redirect(array('action' => 'registration'));
}
}
}
My model name is different which's name is User.php
I want to insert the record using this above code.Any idea how to insert?
you can do this by loading the users model in current controller just write the following line
$this->loadModel('Name of the Model').
then
$this->nameofmodel->save()
As you are unable to understand see this
Controller::loadModel(string $modelClass, mixed $id)¶
The loadModel() function comes handy when you need to use a model which is not the controller’s default model or its associated model:
$this->loadModel('Article');
$recentArticles = $this->Article->find(
'all',
array('limit' => 5, 'order' => 'Article.created DESC')
);
$this->loadModel('User', 2);
$user = $this->User->read();
Above pasted code is taken from CookBook of Cakephp, if you still do not understand just read it it has complete detailed explanation you can also see this to understand
you can use it with $uses variable in SignupController
class SingupController extends AppController
{
public $uses = array('User');
//rest of stuff
}
Or, if you want, you can load it on-demand inside a method:
$this->loadModel('User'); //now model is loaded inside controller and used via $this->User
EDIT: Your data array has to include the name of the model you're saving. So, replace:
$this->Form->create('Signups',array('action' => 'registration')
with:
$this->Form->create('User',array('url' => array('controller' => 'signups', 'action' => 'registration'));
I am trying to add links to a Tree output list.
In addition to the links I get the  's included in the output
So that it looks like this:
**My Categories
Fun
Sport
Surfing
Extreme knitting**
etc.....
I don't want that obviously, but I do want to keep the nested output relationship.
Below is code:
Controller
<?php
class CategoriesController extends AppController {
public $helpers = array('Html', 'Form');
public function index() {
$this->set('output', $this->Category->generateTreeList(null, null, null, ' '));
}
}
?>
View
<?php foreach ($output as $data): ?>
<ul>
<?php echo $this->Html->link($data,
array('controller' => 'data', 'action' => 'view', $data)); ?>
</ul>
<?php endforeach; ?>
<?php unset($data); ?>
You should use a tree helper to output your tree as ul/li including links.
See http://www.dereuromark.de/2013/02/17/cakephp-and-tree-structures/
generateTreeList() - as documented - is a quick way to create a dropdown select ready list, not a tree.
I want to pass values through
<a href= <?php $this->baseUrl()/admin/registration/activate/> </a>
this is my code an i have to pass user Id to this url to activate user. what I have to use I wll get my user id <?php $this->userid; ?>
please help
my action is
public function activateAction()
{ // Administrator actvate user
$user_name = $this->getRequest()->getParam('user_name');
$reg = new clmsRegistrationModel();
$reg->setActive($user_name);
}
inside your action
$this->_request->getParam('prop_id', null);
Lets assume you have Controller Index and Action Customer, so you can build you link with an Zend Framework View helper (inside your view script):
echo $this->url(array(
'controller' => 'index',
'action' => 'customer',
'prop_id' => 5
));
in your Controller:
customerAction() {
$propId = $this->getRequest()->getParam('prop_id');
}
After your comment (add to your view script):
<?php $postUrl = $this->url(array(
'controller' => 'admin',
'action' => 'registration',
'activate' => $this->userid
)); ?>
Activate
How can I add a class to the active navigation link? If a link points to URI /index/index and the request URI is also /index/index, I would like the link to have class, for example:
<li class="active">
Index
</li>
This is how I am initializing navigation in the bootstrap:
protected function _initNavigation()
{
$navigation = new Zend_Navigation($this->getOption('navigation'));
$this->view->navigation($navigation);
}
Ok,
I have solved this by writing a controller plugin:
<?php
class My_Controller_Plugin_PrepareNavigation extends Zend_Controller_Plugin_Abstract
{
public function routeShutdown(Zend_Controller_Request_Abstract $request)
{
$viewRenderer = Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer');
$viewRenderer->initView();
$view = $viewRenderer->view;
$container = new Zend_Navigation(Zend_Registry::get('configuration')->navigation);
foreach ($container->getPages() as $page) {
$uri = $page->getHref();
if ($uri === $request->getRequestUri()) {
$page->setClass('active');
}
}
$view->navigation($container);
}
}
This is how to create a navigation() in a layout() with zend frameworks using Application.
Well, at least one way of doing it. the CSS class is set on the
put this into the Bootstrap.php file:
protected function _initNavigation()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
include APPLICATION_PATH . '/layouts/scripts/menu.phtml';
$view->navigation($container);
}
This allows you to create an array for a menu in the file menu.phtml, so that you can still maintain the active class on the current link. For some strange reason, if you use this you must include the controller property in the array to get the CSS active class on the current link.
put something like this into the /layouts/scripts/menu.phtml file:
$container = new Zend_Navigation(array(
array(
'label' => 'HOME',
'id' => 'tasks',
'uri'=>'/',
'controller' => 'Index'
),
array(
'label' => 'Contact',
'uri' => 'contact',
'controller' => 'Contact'
),
.... more code here ...
put this into the layout.phtml file:
$options = array('ulClass' => 'menu');