Is there a way to use hash mark in Phalcon route pattern? - php

Is there a way to use hash mark in route pattern? I tried to use backslash before hash mark \#, but no result.
My code:
use Phalcon\Mvc\Router\Group;
$gr = new Group([
'module' => 'home',
]);
$gr->addPost("/item/view/([0-9]*)/#([0-9]*)", [
'module' => 'item',
'controller' => 'view',
'firstId' => 1,
'secondId' => 2,
])->setName('item:view:hash');
$router->mount($gr);
Usage:
echo $this->url->get(['for' => 'item:view:hash', 'firstId' => 1, 'secondId' => 2])
gives me a correct url: /item/view/1/#2, but I receive a warning:
Unknown modifier '('
Is there a way to remove warnings, to use the hash mark in the right way? Thanks in advance.

Nothing after the # mark is sent to the server, so including it in a server-side route doesn't do anything. The fragment/anchor is client-side only.

Related

Phalcon PhP - how to use named routes inside a controller

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.

Phalcon URL generator mixes parameters in custom route

I'm trying to get the following route to work:
$router->add('/([a-z]{2})/:namespace/:controller/:action/([^\/]+)', array(
'language' => 1,
'namespace' => 2,
'controller' => 3,
'action' => 4,
'location' => 5
))->setName('location');
The relevant (and for testing purposes only) line in the Volt template looks like this:
{{ url({'for': 'location', 'namespace': 'weather', 'controller': 'forecast', 'action': 'precipitation', 'location': 'Hamburg' }) }}
What I want is //weather/forecast/precipitation/Hamburg but instead all I get is //weather/forecast/precipitation/.
Next thing I tried was
$router->add('/([a-z]{2})/:namespace/:controller/:action/{location:[^\/]+}', array(
'language' => 1,
'namespace' => 2,
'controller' => 3,
'action' => 4,
))->setName('location');
which at least gives me the location in the URL, but at a totally wrong position: //Hamburg/forecast/precipitation/.
Now I've looked into the Library\Mvc\Router and the array that is passed to get() looks fine to me:
Array
(
[for] => location
[namespace] => weather
[controller] => forecast
[action] => precipitation
[location] => Hamburg
[language] => en
)
I will use my own Router to handle translated URLs, so I think we can ignore the language parameter for now. So far, the custom Router does nothing more than call the original one.
Any idea how to get the location parameter to work?
I've tested all possibilities given in the router documentation and even take a look in the router implementation for Phalcon 2.0 (are you using this version, right?!). The only thing that worked out was to not use named parameters at all, even the built-in placeholders needed to be removed from the route pattern:
$this->add('/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_]+)/([^\/]+)', array(
'namespace' => 1,
'controller' => 2,
'action' => 3,
'location' => 4,
))->setName('location');
In other words, this seems like a bug to me. Feel free to file a bug to investigate further.
About the language parameter, I don't know if you already saw this question, but you can deal with this in a similar way.

Not able to fetch Get params in production

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

Zend Url View Helper not working with Regex Routes

I'm using Zend Framework 1.12 and have this route:
$router->addRoute('item_start',
new Zend_Controller_Router_Route_Regex(
'(foo|bar|baz)',
array(
'module' => 'default',
'controller' => 'item',
'action' => 'start'
),
array(
1 => 'area'
),
'%s'
)
);
Problem is, when I call '/foo' and use the Url Helper in the View, it doesn't give me any parameters:
$this->url(array("page"=>1));
// returns '/foo' (expected '/foo/page/1')
$this->url(array("page"=>1), "item_start", true);
// also returns '/foo'
Any idea how to get the page-parameter into the URL? I can't use the wildcard like in the standard route, can't I?
In addition to David's suggestions, you could change this route to use the standard route class, and then keep the wildcard option:
$router->addRoute('item_start',
new Zend_Controller_Router_Route(
':area/*',
array(
'module' => 'default',
'controller' => 'item',
'action' => 'start'
),
array(
'area' => '(foo|bar|baz)'
)
)
);
// in your view:
echo $this->url(array('area' => 'foo', 'page' => 1), 'item_start');
Your Regex route doesn't have a page parameter, so when the url view-helper ends up calling Route::assemble() with the parameters you feed it, it ignores your page value.
The two choices that come to mind are:
Modify your regex to include a (probably optional with default value) page parameter
Manage the page parameter outside of your route in the query string.

CakePHP custom route w/ %20 in url want to replace w/ "-" not sure how to make this happen in the route

My link:
echo $link->link($planDetailsByCompany['PlanDetail']['name'],
array('controller' => 'plan_details', 'action' => 'view_benefit_schedule',
'id' => $planDetailsByCompany['PlanDetail']['id'],
'slug' => $planDetailsByCompany['PlanDetail']['name']));
My custom route:
Router::connect('/pd/:id-:slug',
array('controller' => 'plan_details', 'action' => 'view_benefit_schedule'),
array('pass' => array('id', 'slug'),
'id' => '[0-9]+'));
My url is displaying like so:
..pd/44-Primary%20Indemnity
I cannot determine how to remove the %20 and replace it with a "-". There is a space in the company name that is causing this. Is this possible within the CakePHP router functionality? If so, how? Or another method.
Geeze.. I just solved this!
In my link above, replace the 'slug' line with:
...'slug' => Inflector::slug($planDetailsByCompany['PlanDetail']['name'])...
The Inflector handles the spaces in the url. And my result url is:
...pd/44-Primary_Indemnity

Categories