Is it possible to pass multiple parameters to an action in the controller by using the method postLink of the FormHelper?
I didn't manage to do it by using the options array. It is not very well specified in the docs what values this array admits.
This is what I tryed:
$this->Form->postLink($staffUser['User']['_name'], array(
'action' => 'subscribe',
array('ticketId' => $ticket['Ticket']['id'], 'userId' => $staffUser['User']['id'])
));
My subscribe action looks like this:
public function subscribe($ticketId, $userId = null){
if ($this->request->is('post')) {
//...
}
}
Update
I've just noticed the provided solution creates another problem to me. Now the class stop getting added ulike before when I was using only one parameter:
$this->Form->postLink($staffUser['User']['_name'], array(
'action' => 'subscribe',
$ticket['Ticket']['id'],
$staffUser['User']['id'],
array('class' => 'demo') //not beind added
));
postLink takes its URL entry in the same form as the create method. Parameters don't need a key, put them in order after the action key/value.
Other options need to go in the third argument - you have the options array as an additional entry to the url array.
$this->Form->postLink($staffUser['User']['_name'], array(
'action' => 'subscribe',
$ticket['Ticket']['id'],
$staffUser['User']['id']
),
array(
'class' => 'demo'
)
);
See options for FormHelper::create here:
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
Related
I have developed API's with Zf2 and DynamoDB, I am able to get values from GET params in my local machine but unable to get values from GET params when I uploaded the API's in production
.
FYI: POST method is working properly in production.
Below is the controller get function.
public function get($id)
{
$abcModel = new ABCModel();
error_log("tournamentId:".$this->params()->fromQuery('tournamentId') );
$query = $this->getRequest()->getQuery();
error_log("tournamentId1:".$query['tournamentId']);
error_log("tournamentId2:".$this->getEvent()->getRouteMatch()->getParam('tournamentId'));
error_log("tournamentId3:".$this->params('tournamentId'));
error_log("tournamentId4:".$this->params()->fromRoute('tournamentId'));
}
I have tried all the answers of this question ZF2: Get url parameters in controller.
Can any one know what could be the reason for this?
Any light on the path would be helpful.
To use query string in the production environment, you have to use some alternate approach.
You can add parameter along with route to hold the query string value. But the query string needs to be passed using "/" mark between the route and query string instead of using "?" mark.
/route-name/key=value&key=value1
and the routing configuration need to be
'router' => array(
'routes' => array(
'<route-name>' => array(
'type' => 'segment',
'options' => array(
'route' => '<route-name>[/:action][/:queryPara][/]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'queryPara' => '[a-zA-Z][a-zA-Z0-9_-&=]*',
),
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
)
),
),
)),
You can create a function that will extract the query string and returns the array containing key=>value pairs of query string.
And in controller you have to call the function by passing the query string which will be stored in "/queryPara" part after the route name using the following statement you will get the string
$this->params('queryPara')
Hope it helps
Thanks
I have some simple pagination set up in my controller like so:
public function index() {
$this->set('items', $this->paginate());
}
In my view I'm using the Paginator helper to output numbered links:
echo $this->Paginator->numbers(array(
'separator' => '',
'tag' => 'li',
'currentTag' => 'a',
'currentClass' => 'active'
));
This all works fine, however I want to use a custom URL for the paginated links. I added this to my routes.php file:
Router::connect('/things/:page', array('controller' => 'things', 'action' => 'index'), array('page' => '[0-9]+'));
Now the links outputted by the Paginator helper are the way I want. They look like http://mysite.com/things/2, http://mysite.com/things/3 etc.
But when I click the links the Paginator in my controller doesn't seem to recognize it's on a certain page, as I just get the first page's results shown. I'm guessing I need to somehow pass the page number to $this->paginate(), but I don't know what the best method is. Is there a way for Cake to get the page number automatically if I modify my route?
Thanks!
Since the Pagination component by default expect named parameter 'page:1' you need somehow to pass same variable in index.
if you make print of $this->request->params in your controller, you will see that it's missing.
See that example how you can pass named parameters in the Router:
Router::connect(
'/:controller/:action/*',
array(),
array(
'named' => array(
'wibble',
'fish' => array('action' => 'index'),
'fizz' => array('controller' => array('comments', 'other')),
'buzz' => 'val-[\d]+'
)
)
);
For more info see this section in Cakephp book
I have one route that looks like this:
Router::connect('/Album/:slug/:id',array('controller' => 'albums', 'action' => 'photo'),array('pass' => array('slug','id'),'id' => '[0-9]+'));
and another like this:
Router::connect('/Album/:slug/*',array('controller' => 'albums','action' => 'contents'),array('pass' => array('slug')));
for what doesn't match the first. In the 'contents' action of the 'albums' controller, I take care of pagination myself - meaning I retrieve the named parameter 'page'.
A URL for the second route would look like this:
http://somesite.com/Album/foo-bar/page:2
The Above URL indeed works, but when I try to use the HTML Helper (url,link) to output a url like this, it appends the controller and action to the beginning, like this:
http://somesite.com/albums/contents/Album/foo-bar/page:2
Which i don't like.
The code that uses the HtmlHelper is as such:
$html->url(array('/Album/' . $album['Album']['slug'] . '/page:' . $next))
See below url it is very help full to you
http://book.cakephp.org/2.0/en/development/routing.html
Or read it
Passing parameters to action
When connecting routes using Route elements you may want to have routed elements be passed arguments instead. By using the 3rd argument of Router::connect() you can define which route elements should also be made available as passed arguments:
<?php
// SomeController.php
public function view($articleId = null, $slug = null) {
// some code here...
}
// routes.php
Router::connect(
'/blog/:id-:slug', // E.g. /blog/3-CakePHP_Rocks
array('controller' => 'blog', 'action' => 'view'),
array(
// order matters since this will simply map ":id" to $articleId in your action
'pass' => array('id', 'slug'),
'id' => '[0-9]+'
)
);
And now, thanks to the reverse routing capabilities, you can pass in the url array like below and Cake will know how to form the URL as defined in the routes:
// view.ctp
// this will return a link to /blog/3-CakePHP_Rocks
<?php
echo $this->Html->link('CakePHP Rocks', array(
'controller' => 'blog',
'action' => 'view',
'id' => 3,
'slug' => 'CakePHP_Rocks'
));
Is it possible to pass parameters via redirect? I tried a lot of options, but nothing seems to work. My latest approach is:
return $this->redirect(array('Users::helloworld', 'args' => array('id' => 'myId')));
then I created a route:
Router::connect('/Users/helloworld?id={:id}', array('controller' => 'Users', 'action' => 'helloworld'));
but all I get is users/helloworld/myId
args is part of the routes and will be converted into an URL using the very generic route (not the one you created and don't require)
To get a query string, simply use the ? key:
return $this->redirect(array(
'Users::helloworld',
'?' => array('id' => $myId)
));
// will use the route:
// /{:controller}/{:action}/{:args}
// and generate
// /users/helloworld?id=$myId
The test for that: https://github.com/UnionOfRAD/lithium/blob/master/tests/cases/net/http/RouteTest.php#L374-405
Instead of defining a separate route to pass arguments, you could just do the following.
Lets say you want to pass 2 arguments: $arg1 & $arg2 to the helloworld action of your Users controller:
return $this->redirect(array(
'Users::helloworld',
'args' => array(
$arg1,
$arg2
)
));
I have the following route:
Router::connect('/admin/login/:to',
array('admin'=>true,'controller'=>'users','action'=>'login'),
array(
'to' => '[A-Za-z0-9\._-]+',
'pass' => array('to')
));
Which basically passes a string/int with the login url. But it no longer uses the named parameter of to. So for example instead of getting: /admin/login/to:1AB I get /admin/login/1AB
How do I keep the named parameter but still alter the routing to remove the users bit from the url? I've tried: '/admin/login/to::to' but that seems rather sloppy...
you could find passed parameter's name in "$this->data" in your controller.
in your example: $this->data->to has the same value you put in your url.
Remove that route. Why do you have that route when you want the named parameter?
Edit: if so:
Router::connect(
'/admin/login/*',
array(
'admin' => true,
'controller' => 'users',
'action' => 'login'
)
);