Hi Guys,
Firstly, this is my first post, so please be gentle and my apologies
for any inevitable faux pas should I make any. Secondly, I am aware
there seems to be much confusion around redirect, I have been studying
both the Cookbook, CakePHP Application Development (Book) and a series
of tutorials in Linux Format. I have been learning for a couple of
months now - though fully admit to being a newbie.
I'm expecting my problem to be simple, and down to a basic lack of
understanding of redirect, so again, I apologise if this is answered
elsewhere - if it is and I've read it, I haven't understood it.
I have a series of resources, each belonging (HABTM) to a subcategory,
each subcategory belongs to one category. At the start of the App you
click on the title of the category, which then filters by passing,
using custom routing, the name of the category to the subcategory
controller eg:
// Provides the category title to the subcategory index/list page
Router::connect(
'/subcategories/:category',
array('controller' => 'subcategories', 'action' => 'index'),
array(
'pass' => array('category')
)
);
function index($category = null) {
if (!empty($category)) {
$this->set('category', $category);
$conditions = array(
'conditions' => array('Category.title'
=> $category),
'order' => 'Subcategory.order
ASC'
);
$subcategories = $this->Subcategory->find('all', $conditions);
} else {
$this->set('category', 'All');
$conditions = array(
'order' => array(
'Category.title' => 'ASC',
'Subcategory.order' => 'ASC'
)
);
$subcategories = $this->Subcategory->find('all', $conditions);
}
$this->Subcategory->recursive = 0;
$this->set('subcategories', $subcategories);
}
*This code is only relevant for background info, not the source of the
fault.*
This works well. Each link provides the data in the correct url format
to each subsequent 'thing', subcategory index or resource index.
The problem I have is with editing. When I need to Edit a Subcategory
(for example, this will need to work for Resources also) I pass
parameters (category being the one used to filter, so the one I'm most
interested in) to the edit controller, and in turn to the view like
so:
>From subcategory index.ctp:
<?php echo $html->link('Edit', array(
'controller' => 'subcategories',
'action' => 'edit',
'id' => $subcategory['Subcategory']['id'],
'category' => $category
)); ?>
Which is received well enough by the edit controller (I display my
category in the edit view as a test).
So I know my controller has the variable at it's disposal.
function edit($id = null, $category = null) {
$this->set('category', $category);
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid subcategory', true));
$this->redirect(array('action' => 'index'));
}
if (!empty($this->data)) {
if ($this->Subcategory->save($this->data)) {
$this->Session->setFlash(__('The subcategory has been
saved',
true));
$this->redirect(array(
'controller' => 'subcategories',
'action' => 'index',
'id' => $id,
'category' => $category
));
} else {
$this->Session->setFlash(__('The subcategory could not
be saved.
Please, try again.', true));
}
}
if (empty($this->data)) {
$this->data = $this->Subcategory->read(null, $id);
}
$categories = $this->Subcategory->Category->find('list');
$resources = $this->Subcategory->Resource->find('list');
$this->set(compact('categories', 'resources'));
}
Please let me draw your attention to the redirect line after a
successful update. The URL format I am hoping for is this:
http://domain/subcategories/categoryvariable
What I actually get is
http://domain/subcategories/index/id:edit/category:4d410479-4fb0-4ad8-a0be-6716a9baf6a0
(where the UUID is the id of the record).
It would be fair to ask why I pass the id ('id' => $id) if I don't
need it. If I don't include it there, I'm redirected to the edit view,
not the index at all.
I'm keen to assist on this element of the CakePHP documentation, and
once I understand redirect I think that article could be enhanced to
cover sending parameters (unless I've not found it in the docs,
whereby a link to the solution could be added).
Can anyone please help me with this? I would be extremely grateful.
Adam
--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others
with their CakePHP related questions.
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php