I am passing some params in url as below:
sample/team/highestScore/1
My router code for accepting this URL is:
$this->add('/sample/team/{tab:[a-zA-Z0-9-_]+}/{matchType:[0-9]+}',array('action' => 'teamAction'))->setName('sample');
But in controller I am getting the value of param 'tab' as highestscore, means in lowercase. I need the param value as highestScore. How can I get the value without case conversion.
Please advise. Thanks in advance.
Controller: (app/controllers/someController.php)
function teamAction($tab = null, $matchType = null)
{
exit(var_dump([
$tab,
$matchType
]));
}
route ( app/config/router.php )
$router = $di->getRouter();
$router->add(
'/sample/team/{tab:[a-zA-Z0-9-_]+}/{matchType:[0-9]+}',
[
'controller' => 'some',
'action' => 'team'
]
);
$router->handle();
testing url phalcon_path/sample/team/highestScore/1
array (size=2)
0 => string 'highestScore' (length=12)
1 => string '1' (length=1)
Related
I am using Zend 1.10. I am trying to create a route for unlimited variables like abc.com/var1/var2/var3 and so on.
Till now i have searched and find out that i can add route for each variable like
$route = new Zend_Controller_Router_Route(
'/emaillog/:var1',
array(
'controller' => 'emaillog', // The controller to point to.
'action' => 'index', // The action to point to, in said Controller.
':var1' =>null //default value if param is not passed
)
);
$frontController->getRouter()->addRoute('emaillogWithVar1', $route);
and for second variable,
$route = new Zend_Controller_Router_Route(
'/emaillog/:var1/:var2',
array(
'controller' => 'emaillog', // The controller to point to.
'action' => 'index', // The action to point to, in said Controller.
':var1' =>null, //default value if param is not passed
':var2' =>null //default value if param is not passed
)
);
$frontController->getRouter()->addRoute('emaillogWithVar2', $route);
But the work i going to do, can contain 1-∞ infinite variables.
So i want to have once route for unlimited variables.
Any help will be appreciated!
There is a way to get infinite params. For that you don't have to declare them.
Lest say the url is index/test/p1/p2/p3/p4/p5, now if you try the following inside IndexController:
public function testAction()
{
var_dump($this->getRequest());
}
You will get a dump of an instance of Zend_Controller_Request_Http, in that dump, you will see, 2 properties, _params & _pathInfo, Like:
protected '_pathInfo' => string '/index/test/p1/p2/p3/p4/p5' (length=26)
protected '_params' =>
array (size=5)
'controller' => string 'index' (length=5)
'action' => string 'test' (length=4)
'p1' => string 'p2' (length=2)
'p3' => string 'p4' (length=2)
'module' => string 'default' (length=7)
1st method: Now in _params, they will occur as key => value pairs, as long as they are even, the last odd param will be ignored. You can get these by(for example):
$params = $this->getRequest()->getParams();
unset($params['controller']);
unset($params['action']);
unset($params['module']);
$params = array_merge(array_keys($params), array_values($params));
var_dump($params);
Output:
array (size=4)
0 => string 'p1' (length=2)
1 => string 'p3' (length=2)
2 => string 'p2' (length=2)
3 => string 'p4' (length=2)
2nd Method: OR you can use _pathinfo to get the params using explode, like:
$path = $this->getRequest()->getPathinfo();
$params = explode('/', $path);
unset($params[0]);
unset($params[1]);
unset($params[2]);
var_dump($params);
Output:
array (size=5)
3 => string 'p1' (length=2)
4 => string 'p2' (length=2)
5 => string 'p3' (length=2)
6 => string 'p4' (length=2)
7 => string 'p5' (length=2)
I'm having trouble finding how to get the Urls from named routes inside a Phalcon PhP controller. This is my route:
$router->add(
'/admin/application/{formUrl:[A-Za-z0-9\-]+}/{id:[A-Za-z0-9\-]+}/detail',
[
'controller' => 'AdminApplication',
'action' => 'detail'
]
)->setName("application-details");
I want to get just the Url, example: domain.com/admin/application/form-test/10/detail . With the code below I can get the html to create a link, the same result of the link_to.
$url = $this->tag->linkTo(
array(
array(
'for' => 'application-details',
'formUrl' => $form->url,
'id' => $id
),
'Show'
)
);
The result I want is just the Url. I'm inside a controller action. I know it must be really simple, I just can't find an example. Can you help me?
Thanks for any help!
You should use the URL helper. Example:
$url = $this->url->get(
[
'for' => 'application-details',
'formUrl' => $form->url,
'id' => $id,
],
[
'q' => 'test1',
'qq' => 'test2',
]
);
You can pass second array for query string params if needed.
According to your route definition, the above should output something like:
/admin/application/form-url/25/detail?q=test1&qq=test2
More info of Generating URIs in the docs.
Purpose: to redirect a specific route with an array value. I am not able to use View::make in my situation, which causes problem.
$value = 'Sarah';
$array_param = array(
'1' => 'a',
'2' => 'b'
);
return Redirect::route('myroute', array(
'name' => $value
));
Above is cool. But i cannot use $array_param with redirect route, which expects a string parameter, but i'm sending an array variable. Alternative way?
return Redirect::route('myroute', array(
'name' => $value,
'parameter' => $array_param
));
--update--
Route::post('myroute/{name}/{array_param}', array(
'as' => 'myroute',
'uses' => 'mycontroller#mymethod'
));
What the version of Laravel do you have?
The code below works for me correctly on laravel 5.1. Maybe it'll help you.
public function store(Request $request)
{
$item = Item::find(1); // an example
return redirect()->route('item.show', ['id' => $item->id]);
}
and yes, the redirect to the post route looks very incorrect. Please try to use the redirect only to the GET routes.
So here's my issue:
I am fetching data in my database and want to provide them in jSON format.
My controller is the following:
public function testAction()
{
$articles = Article::find();
if (count($articles) > 0) {
$final_array = array();
foreach ($articles as $article) {
$user = Users::find("id = " . $article->getUsersId());
$current = array('id' => $article->getId(),
'name' => $article->getName(),
'replies' => $article->getReplies(),
'date' => $article->getDate(),
'illustration' => $article->getIllustration(),
'content' => $article->getContent(),
'link' => $article->getLink(),
'user_id' => $article->getUsersId(),
'user_name' => $user[0]->getPseudo());
$final_array[] = $current;
}
$result = array('status' => 1,
'message' => 'article have been downloaded',
'response' => $final_array);
} else {
$result = array('status' => 1,
'message' => 'no article in the stack');
}
$this->view->disable();
$this->response->setContentType('application/json', 'UTF-8');
echo json_encode($result);
}
The view displayed provide nothing:
<html>
<head></head>
<body></body>
</html>
The trouble doesn't come from my model or SQL request, because if I var_dump my result instead by changing my controller like this:
[...]
//$this->view->disable();
//$this->response->setContentType('application/json', 'UTF-8');
var_dump($result);
[...]
It provides me the following (length doesn't match all the time because I voluntary changed the content which is not interesting in this case):
array (size=3)
'status' => int 1
'message' => string 'article have been downloaded' (length=28)
'response' =>
array (size=1)
0 =>
array (size=9)
'id' => string '1' (length=1)
'name' => string 'Champion de CAPU' (length=16)
'replies' => string '0' (length=1)
'date' => string '2014-06-10 06:22:35' (length=19)
'illustration' => string 'illustration_link' (length=69)
'content' => string 'content_text' (length=182)
'link' => string 'more_link' (length=50)
'user_id' => string '6' (length=1)
'user_name' => string 'bathiatus' (length=9)
which is what I want to get...
Moreover, I actually did the same in another controller in order to provide all the users (so UserController, jGetAllUsersAction) and it works pretty well (the code is the same except that the table in the database are different).
I finally figured out the issue.
Thank you for the answers, I found that all purposed way to display the view is working (including the one I purposed in my question).
I don't really know which way is the best, but I guess the one in my question is not.
By the way, the issue was that I was trying to inject special characters (such as é, è, à, ë, ...) in my Json object. Indeed, my content is in french.
Json object do not support these kind of characters while printing them with a var_dump presents no issue.
If you want just a json response you can do this in your controller:
return $this->response->setJsonContent($result);
Phalcon disables the view and sets the right content type automatically with that. The json_encode is also done, so just put in your $result.
By default Phalcon needs a view to return the response (in your case the JSON).
Controller code:
$this->response->setContentType('application/json', 'UTF-8');
$this->view->setVar("some_var", $result);
Then you have to create a view corresponing to the name of the controller and the function
and put there:
<?php echo json_encode($some_var); ?>
If you don't want to crate a additional view, please use this link for further reference:
http://docs.phalconphp.com/en/latest/reference/response.html
I'm using yaf php framework
I want to get param array.
ex:
My url:... mvcSample/svc/saveUser/user1/pass/a#b.c/joe/foo
My output params dump:
array (size=3)
'user1' => string 'pass' (length=4)
'a#b.c' => string 'joe' (length=3)
'foo' => null
I want:
array (size=5)
1 =>string 'user1'
2 => string 'pass'
3 => string 'a#b.c'
4 => string 'joe'
5 =>string 'foo'
How to change default url pattern ?
Thank you
Since Yaf is very well undocumented, the only option I can think about is to parse URI by Your own:
$parts = explode('/', $this->getRequest()->getRequestUri());
But You will get garbage on begining of $parts array.
Propably, sooner or later You will start to trying custom URL routing (some samples You can find here: http://docs.php.net/manual/da/yaf-route-regex.construct.php), which will allow You to parse URLs using regexps and pass matched groups to controller:
/* in Bootstrap.php */
$dispatcher->getRouter()->addRoute('saveUser',
new Yaf_Route_Regex(
',^/mvcSample/svc/saveUser/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$,',
array(
'controller' => 'svc',
'action' => 'saveUser',
),
array(
1 => 'username',
2 => 'password',
3 => 'email',
4 => 'firstname',
5 => 'somefooshmoo',
),
)
);