I have a site developed in cakephp 2 and I want that into my default.ctp there is a menu with some elements taked from the database.
How can I change the elements which controller I have to use? Where I have to put my query? Because if I was into a model is easy but into the default.ctp? How can I do that?
Here is my default.ctp:
<body>
<div class="content">
<div id="navigation">
<ul>
<?php
if (!empty($authUser)) {
?>
<li><?php echo $this->Html->link('I want to change this', array('controller' => 'ingredients', 'action' => 'index')); ?></li>
<li><?php echo $this->Html->link('I want to change this', array('controller' => 'brands', 'action' => 'index')); ?></li>
<li><?php echo $this->Html->link('I want to change this', array('controller' => 'manufacturers', 'action' => 'index')); ?></li>
<?php
// $is_logged from UsersController->beforeFilter
echo $this->element ('header_menu_logged');
} else {
?>
<li><?php echo $this->Html->link('Competizioni', array('controller' => 'brands', 'action' => 'index')); ?></li>
<li><?php echo $this->Html->link('Cerca', array('controller' => 'manufacturers', 'action' => 'index')); ?></li>
<?php
// $current_model
echo $this->element ('header_menu', array('selected' => 'Pippo'));
}
?>
</ul>
</div>
<div class="page">
<?php
// messaggi di stato per le azioni
if (empty($flash_element)) {
$flash_element = $this->Session->read('flash_element');
if (empty($flash_element)) {
$flash_element = 'default';
}
}
// echo '>'.$flash_element.'<';
$auth_msg = $this->Session->flash('auth', array ('element' => 'flash_'.$flash_element));
$flash_msg = $this->Session->flash('flash', array ('element' => 'flash_'.$flash_element));
if (!empty($auth_msg)) {
echo $auth_msg;
}
if (!empty($flash_msg)) {
echo $flash_msg;
}
?>
<section><!--class="contents"-->
<?php echo $content_for_layout; ?>
</section>
</div>
</div>
</body>
Well, I suggest doing $this->set('anyString', $varStringOfTheLink); in your beforeRender() or beforeFilter() in appController.php .
Do your query from there and then use the set() function to set a variable for your view.
Then in default.ctp you will be able to use $anyString.
So to do a quick wrap-up. Do the query in your appController, before filter or before render, then set it so your view can use it. The first parameter of the set() function is the name of the var you want to use in your view. Just put a $ before. The second paremeter is the value of the variable you want.
//appController.php
function beforeFilter(){
$myQueryVar = $this->Model->find('whateverIWant');
$this->set('myLinkOne', $myQueryVar);
}
//layouts/default.ctp
echo $this->Html->link($myLinkOne, array('controller' => 'ingredients', 'action' => 'index'));
Related
I am trying to add a child in a parent view in CakePHP.
Meaning, I have a Lesson I want to add to a Course in the Course view.
I tried doing it via a simple form helper - adding a $course['Lesson']:
<div class="lessons form">
<?php echo $this->Form->create($course['Lesson']); ?>
<fieldset>
<legend><?php echo __('Add Lesson'); ?></legend>
<?php
echo $this->Form->input('name');
echo $this->Form->input('description');
echo $this->Form->input('course_id', array(
'value'=>$course['Course']['id']
));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
But that doesn't seem to do it.
Am I missing something in the Controller/Model?
let's think course controller
//course controller
public function add_lesson() {
if ($this->request->is('post')) {
$this->Lesson->create();
if ($this->Lesson->save($this->request->data)) {
$this->Session->setFlash(__('The Lesson has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The Lesson could not be saved. Please, try again.'));
}
}
$this->set('courses', $this->Course->find('list'));
}
//add_Lesson View
<?php echo $this->Form->create('Lesson', array('url' => array('controller'=>'courses', 'action'=>'add_lesson')) ); ?>
<fieldset>
<legend><?php echo __('Add Lesson'); ?></legend>
<?php
echo $this->Form->input('name');
echo $this->Form->input('description');
echo $this->Form->input('course_id');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
If you want to use Course controller you can save your data like this:
$this->Course->Lesson->save($this->request->data);
in this case you will need to make sure after submitting your form your data is structure as below:
array(
'Course' => array(
'id' => 1
),
'Lesson' => array(
'name' => 'name'
'description' => 'balbalbala'
'course_id' => 1
),
);
I have 2 templates,
- /view/opinions/add.ctp - add form
- /view/opinions/list.ctp - displays opinions
and I want them diplay in /views/opinions/index.ctp is it possible?
Is the only way to do it by $this -> element() ? if so, can I include templates from /view/opinions instead of /view/elements ?
#edit
OpinionsController.php
class OpinionsController extends AppController {
public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session');
var $name = 'Opinions';
function index() {
$opinions = $this->Opinion->find('all');
if(isset($this->params['requested'])) {
return $opinions;
}
$this->set('opinions', $opinions);
}
public function add() {
if ($this->request->is('post')) {
$this->Opinion->create();
if ($this->Opinion->save($this->request->data)) {
$this->Session->setFlash(__('saved.'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('Unable to add.'));
}
}
}
}
index.ctp
$this->extend('/Opinions/view');
$this->extend('/Opinions/add');
add.ctp
echo $this->Form->create('Opinion', array('type' => 'file'));
echo $this->Form->input('author_name', array('label' => 'Imię'));
echo $this->Form->input('author_signature', array('label' => 'Podpis'));
echo $this->Form->input('text', array('rows' => '5', 'cols' => '30', 'label' => 'Opinia'));
echo $this->Form->input('author_pic', array('type' => 'file', 'label' => 'Zdjęcie'));
echo $this->Form->input('author_pic_dir', array('type' => 'hidden'));
echo $this->Form->end('Dodaj opinię');
view.ctp
`
<?php foreach ($opinions as $opinion): ?>
<div class="opinion">
<div class="author">
<div class="pic"><?php echo $this->Html->image("defaultAvatar.jpg", array('alt' => $opinion['Opinion']['author_name'])); ?></div>
<div class="signature"><b><?= $opinion['Opinion']['author_name']?></b><br><i><?= $opinion['Opinion']['author_signature']?></i></div>
</div>
<div class="text">
<blockquote><p><?= $opinion['Opinion']['text']?></p></blockquote>
</div>
<div class="clear"><!-- . --></div>
</div>
<div class="clear"><!-- . --></div>
<?php endforeach; ?>
<?php unset($post); ?>
`
In web frameworks like CakePHP, we want to keep our code DRY. In order to do what you want, I think the better approach is to create elements to list your opinions and another with the form to add a new one.
Then in index.ctp you would put these two elements. And in add.ctp you put only the element with the form and in view.ctp you should put the element that list the opinions.
I am trying to use a foreach statement to display a list ul>li. In case the list has no elements how can i handle that criteria.
<ul>
<?php foreach($person['CastsMovie'] as $cast): ?>
<li><?php echo $this->Html->link($cast['Movie']['name'], array('controller' => 'movies', 'action' => 'view', $cast['movie_id'], 'admin' => true), array('escape' => false)); ?> (<?php echo date("Y", strtotime($cast['Movie']['MovieDetail']['release_date'])); ?>)</li>
<?php endforeach; ?>
</ul>
Check whether it is empty or not:
if ( !empty( $person ) ) {
foreach ( $person as $cast ) {
echo "<li>$cast</li>";
}
} else {
echo "<li>This play has no cast members.</li>";
}
Actually, you need to surround the UL with a check.
Otherwise you end up with an empty "ul" tag which is unnecessary:
<?php if (!empty($person['CastsMovie'])) { ?>
<ul>
<?php foreach($person['CastsMovie'] as $cast) { ?>
<li><?php echo $this->Html->link($cast['Movie']['name'], array('controller' => 'movies', 'action' => 'view', $cast['movie_id'], 'admin' => true), array('escape' => false)); ?> (<?php echo date("Y", strtotime($cast['Movie']['MovieDetail']['release_date'])); ?>)</li>
<?php } ?>
</ul>
<?php } ?>
this means: you check if there are any list elements (in your case CastsMovies to this person) and if so you display the list (ul + li). if not the complete list will be omitted (not just the child li elements).
FIRST, in whatever "controller" you are using, prepare your data, to make it fit for the template.
foreach($person['CastsMovie'] as $key => $cast) {
$cast['html_link'] = $this->Html->link($cast['Movie']['name'],
array('controller' => 'movies',
'action' => 'view',
$cast['movie_id'],
'admin' => true),
array('escape' => false)
);
$cast['html_date'] = date("Y", strtotime($cast['Movie']['MovieDetail']['release_date']));
$person['CastsMovie'][$key] = $cast;
}
Then make your template a sane and readable
<?php if($person['CastsMovie']: ?>
<ul>
<?php foreach($person['CastsMovie'] as $cast): ?>
<li>
<?=$cast['html_link']?> (<?=$cast['html_date']?>)
</li>
<?php endforeach ?>
</ul>
<?php endif ?>
I'm trying to get drop down list in Cake for below two finds by list:
function add() {
if (!empty($this->data)) {
$this->User->create();
if ($this->User->save($this->data)) {
$this->Session->setFlash(__('The User has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
}
$group = $this->User->Group->find('list');
$commune = $this->User->Commune->find('list');
$this->compact('group','commune');
}
}
The model already has belongsTo defined in User model for Group and Commune models with their ids but I cannot seem to get group and commune to show up as drop down list in the add view page.
Below is what I have for add view page.
<div class="Users form">
<?php echo $this->Form->create('User');?>
<fieldset>
<legend><?php __('Add User'); ?></legend>
<?php
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->input('group_id', array('type' => 'select'));
echo $this->Form->input('commune_id');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>
<div class="actions">
<h3><?php __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Users', true), array('action' => 'index'));?></li>
</ul>
</div>
Is there a reason why it's not working?
you need to use plurals in order to automatically load those options into the form fields.
$groups, $communes
Try this in the view:
$this->Form->input('Group');
Alternatively:
$this->Form->input('group_id', array('type' => 'select', 'options' => $group));
You're setting $group and $commune but the view is looking for $group_id and $commune_id.
I am, using a cakephp form helper to upload images to my server. The uploaded file-name gets stored in the DB but i want to store the full file-path on the server because i need this to show in
my json View(result). I tried to accomplish this with a hidden input in my form but that didn't work so im stuck. Please any help! This is my form:
<div class="images form">
<?php // echo $this->Form->create('Image');?>
<?php echo $form->create('Image',array('type' => 'file')); ?>
<fieldset>
<legend><?php __('Add Image'); ?></legend>
<?php
echo $this->Form->input('gallery_id');
echo $this->Form->input('name');
//echo $this->Form->input('img_file');
$form->input('img_file', array('type' => 'file'));
echo $form->input('plaatsfoto', array('value'=>'http://localhost/tut_blog/img/uploads/images/','type' => 'hidden'));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>
<div class="actions">
<h3><?php __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Images', true), array('action' => 'index'));? ></li>
<li><?php echo $this->Html->link(__('List Galleries', true), array('controller' => 'galleries', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Gallery', true), array('controller' => 'galleries', 'action' => 'add')); ?> </li>
</ul>
</div>
You could use CakePHP's beforeSave() function to append the path to the filename before it is saved to the database. This would allow you to manipulate the form data before it is saved to the database. CakePHP beforeSave()
I would also check out these other resources on different methods of uploading and storing files in Cake:
http://www.davidgolding.net/cakephp/using-the-file-functions-in-cakephp.html
http://cakedc.com/florian_kraemer/2010/01/25/file-uploading-file-storage-and-cakephp-mediaview-class
function beforeSave($options) {
if (!empty($this->data['Image']['name'])) {
$this->data['Image']['name'] = "/tut_blog/img/uploads/images/".$this->dateFormatBeforeSave($this->data['Image']['name']);
}
return true;}
Keep in mind that there are better ways to dynamically detect an uploaded file's path rather than putting in a static path like this.