SELECT in cake php - cakephp

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());
}
}

Related

Redirect after creation of Entry in Cakephp 3

I want to redirect to the view of what I just entered in my article. But it seems id is not available somehow...
if ($this->Articles->save($data, array('deep' => true))) {
$current_id = $this->Articles->id;
return $this->redirect(['controller'=> 'articles', 'action' => 'view', $current_id]);
}
I just get 502 Bad Gateway
Sometimes also:
Table "App\Model\Table\ArticlesTable" is not associated with "id"
Apperently you have to save the save function in a variable. Then it works:
if ($this->Articles->save($tmp = $data, array('deep' => true))) {
$current_id = $tmp->id;
return $this->redirect(['controller'=> 'articles', 'action' => 'view', $current_id]);
}
I think right code is
$article= $this->Articles->newEntity($data);
if ($this->Articles->save($article, array('deep' => true))) {
$current_id = $article->id;
return $this->redirect(['controller'=> 'articles', 'action' => 'view', $current_id]);
}

Why doesn't Cakephp Html::link() work as I expect it to?

Using this code:
$sitemap = 4;
$link = $this->Html->link( $sitemap . '.xml', null,
array('plugin' => $this->request->plugin,
'controller' => $this->request->controller,
'action' => 'view',
'admin' => false));
I expect to get a link that looks like this:
http://www.domain.com/vreb_listings/vreb_listing_feeds/view/4.xml
Instead, I get this:
/admin/vreb_listings/vreb_listing_feeds/4.xml
What gives? The action => view is having no effect, as view doesn't show up in the url, and the admin => false isn't working either, as admin does show up. This code is in the admin area.
I haven't even looked up how to include the full domain path in the url. Also, I want the title text to be the same as the url.
According to docs, the second parameter is the url one, so in your code it should be
$link = $this->Html->link( $sitemap . '.xml',
array('plugin' => $this->request->plugin,
'controller' => $this->request->controller,
'action' => 'view',
'admin' => false));
(remove the null second parameter)
Oh, and for title text, that is the third parameter, so
$link = $this->Html->link( $sitemap . '.xml',
array('plugin' => $this->request->plugin,
'controller' => $this->request->controller,
'action' => 'view',
'admin' => false),
array('title' => $sitemap.'.xml'));
should do the trick

How to maintain a variable in the url which is customized in cakephp?

What im trying to do here is maintain the variable 42 all throughout all pagination urls. I want my url to change from this
/exams/take/42/page:2
to this
/exams/take/42/items/2
Again,the number 42 is the variable..and the number 2 is the page number..Thanks.
UPDATE :
routes.php
Router::connect('/examinations/take/:id/page/:page',
array('controller' => 'examinations', 'action' => 'take'),
array(
'pass' => array('id', 'page'),
'id' => '[0-9]+',
'page' => '[0-9]+'
)
);
in the view/take
$this->Paginator->options(array('url' => $this->passedArgs));
AppController.php
public function beforeFilter(){
if (isset($this->request->params['page'])) {
$this->request->params['named']['page'] = $this->request->params['page'];
}
}
ive tried this ..but the generated url is the same,/examinations/take/42/page:2 ,when i click the next and prev links..
You've to define custom routes:
http://book.cakephp.org/2.0/en/development/routing.html
Example as below:
Router::connect(
'/exams/take/:id/items/:number',
array('controller' => 'exams', 'action' => 'take'),
array('pass' => array('id', 'number'))
);
Also you can get more information from below url try this:
http://www.sakic.net/blog/changing-cakephp-pagination-urls
yes you can do it with the use of custom routing. for more undrestanding you can see cakephp manual to manage custom routing in pagination
http://book.cakephp.org/2.0/en/development/routing.html
below will be your
e.g.
And by adding this route:
Router::connect('/:id/page/:page',
array('controller' => 'examinations', 'action' => 'take'),
array(
'pass' => array('id', 'page'),
'id' => '[0-9]+',
'page' => '[0-9]+'
)
);
and i does not work you can refer link

cakephp link without url encoding

CakePHP 2.2.3
I have something like this:
$this->Html->link('here',
array(
'controller' => 'biz',
'action' => 'search',
'range' => '1+3'),
array('escape' => false));
When I click on this link the url will be encoded like this:
/biz/search/range:1%2B3
But I need
/biz/search/range:1+3
Is there any way to switch off url encoding or should I change my controller which parses the named parameter??
Try using:
$this->Html->link('here',
array(
'controller' => 'biz',
'action' => 'search',
'range' => '1\+3'),
array('escape' => '\'));
Simply can you try this
//search.ctp
echo $this->Html->link('here', '/biz/search/range:1+3');
Receive this in controller
//BizController.php
public function search() {
var_dump($this->request->params['named']);
// do something
}

Can't delete posts in forum. (CakePHP)

I'm using the Cupcake Forum plugin within CakePHP. There's a form for selecting the desired posts, and then submitting the form to delete the posts. The form data is apparently being sent to the 'moderate' function within the 'topics' controller using POST and GET methods simultaneously. The function first checks to see if the data sent in is POST. However, when the data is received, it shows that it's GET. A fellow programmer and I don't want to completely change someone else's internal code, but we can't figure out how the data is being sent by both methods and being received as GET. The code from the plugin is below:
--------------moderate.ctp (view)---------------------
<?php echo $form->create('Post', array('url' => array('controller' => 'topics', 'action' => 'moderate', $topic['Topic']['slug']))); ?>
-------------topics_controller.php (controller)-------
public function moderate($id) {
if ($this->RequestHandler->isGet()){
$this->log('Is GET!');
}
$user_id = $this->Auth->user('id');
$topic = $this->Topic->getTopicForViewing($id, $user_id, 'id');
// Access
$this->Toolbar->verifyAccess(array(
'exists' => $topic,
'permission' => $topic['ForumCategory']['accessRead'],
'moderate' => $topic['Topic']['forum_category_id']
));
$this->log('ID: '.$id.'\n');
if ($this->RequestHandler->isPost()){
$this->log('Is POST!');
}
if ($this->RequestHandler->isGet()){
$this->log('Is GET!');
}
$this->log($this->RequestHandler->getReferer());
$this->log(serialize($this->data));
// Processing
if ($this->RequestHandler->isPost()) {
$this->log('INSIDE POST!');
if (!empty($this->data['Post']['items'])) {
$items = $this->data['Post']['items'];
$action = $this->data['Post']['action'];
foreach ($items as $post_id) {
$this->log('Action: '.$action.'\n');
$this->log('PostID: '.$post_id.'\n');
if (is_numeric($post_id)) {
if ($action == 'delete') {
$this->Topic->Post->destroy($post_id);
$this->Session->setFlash(sprintf(__d('forum', 'A total of %d post(s) have been permanently deleted', true), count($items)));
}
}
}
}
}
We added the log checks, which show the result of 'Is GET!' in Cake's log file. Since the method is GET, the statement 'if ($this->RequestHandler->isPost())' is never true; therefore, the submitted posts aren't deleted. What are we missing?
Try changing moderate.ctp to
<?php
echo $form->create('Post', array(
'url' => array(
'controller' => 'topics',
'action' => 'moderate',
$topic['Topic']['slug'],
),
'type' => 'post',
));
?>

Resources