Phalcon URL generator mixes parameters in custom route - php

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.

Related

Realurl generating non default language titles in the url of default tt_news links

I am using TYPO3 7.6.9, tt_news 7.6.1 and realurl 2.0.14 .
I've configured my realurl manually for my multi-language website. The problem is that, real url is generating non-default language titles for news items in the site at default view.
For instance, my default language is bosnian and alternate translation is english.
When I view the page like www.mysite.com/bs/article/news.. url is cool.
But when the url is www.mysite.com/article/news.. then the translation of news title is not shown. Instead english title is shown in url.
Already wrote every possible realurl_conf. :( But still pulling my hair out.
'postVarSets' => array (
'_DEFAULT' => array (
'article' => array(
'news' => array (
'GETvar' => 'tx_ttnews[tt_news]',
'lookUpTable' => array(
'table' => 'tt_news',
'id_field' => 'uid',
'alias_field' => 'title',
'addWhereClause' => ' AND NOT deleted AND NOT hidden',
'useUniqueCache' => 1,
'useUniqueCache_conf' => array(
'strtolower' => 1,
'spaceCharacter' => '-',
),
'expireDays' => 180,
'languageGetVar' => 'L',
'languageExceptionUids' => '',
'languageField' => 'sys_language_uid',
'transOrigPointerField' => 'l18n_parent',
// 'autoUpdate' => 1,
),
),
),
Typoscript is like below
# language configuration
page.config.linkVars = L
page.config.sys_language_uid = 30
page.config.language = bs
page.config.locale_all = bs_BA.utf8
# english language
[globalVar = GP:L = 0]
page.config.sys_language_uid = 0
page.config.language = en
page.config.locale_all = en_GB.utf8
page.config.htmlTag_langKey = en
[global]
I am aware that, during default view no 'L' parameter is passed and all. But with the shared realurl_conf it must be solved,right ??
I've tried valueDefault in vain.
Having no answer to my question yet, I've somehow managed to fix this issue. I know this is quite vague, but I fiiirmly believe the fix came from some typoscript defining the languages. Anyways t is cool now (at least for my website).
Next time you come across an issue like this, try checking your language related typoscript settings. Good Luck Amigos. Peace. :)

understanding ElasticSearch routing

I am trying to use the elasticsearch routing mapping to speed up some queries, but I am not getting the expected result set (not worried about the query performance just yet)
I am using Elastic to set up my mapping:
$index->create(array('number_of_shards' => 4,
'number_of_replicas' => 1,
'mappings'=>array("country"=>array("_routing"=>array("path"=>"countrycode"))),
'analysis' => array(
'analyzer' => array(
'indexAnalyzer' => array(
'type' => 'keyword',
'tokenizer' => 'nGram',
'filter' => array('shingle')
),
'searchAnalyzer' => array(
'type' => 'keyword',
'tokenizer' => 'nGram',
'filter' => array('shingle')
)
)
) ), true);
If I understand correctly, what should happen is that each result should now have a field called "countrycode" with the value of "country" in it.
The results of _mapping look like this:
{"postcode":
{"postcode":
{"properties":
{
"area1":{"type":"string"},
"area2":{"type":"string"},
"city":{"type":"string",
"include_in_all":true},
"country":{"type":"string"},
"country_iso":{"type":"string"},
"country_name":{"type":"string"},
"id":{"type":"string"},
"lat":{"type":"string"},
"lng":{"type":"string"},
"location":{"type":"geo_point"},
"region1":{"type":"string"},
"region2":{"type":"string"},
"region3":{"type":"string"},
"region4":{"type":"string"},
"state_abr":{"type":"string"},
"zip":{"type":"string","include_in_all":true}}},
"country":{
"_routing":{"path":"countrycode"},
"properties":{}
}
}
}
Once all the data is in the index if I run this command:
http://localhost:9200/postcode/_search?pretty=true&q=country:au
it responds with 15740 total items
what I was expecting is that if I run the query like this:
http://localhost:9200/postcode/_search?routing=au&pretty=true
Then I was expecting it to respond with 15740 results
instead it returns 120617 results, which includes results where country is != au
I did note that the number of shards in the results went from 4 to 1, so something is working.
I was expecting that in the result set there would be an item called "countrycode" (from the rounting mapping) which there isn't
So I thought at this point that my understand of routing was wrong. Perhaps all the routing does is tell it which shard to look in but not what to look for? in other words if other country codes happen to also land in that particular shard, the way those queries are written will just bring back all records in that shard?
So I tried the query again, this time adding some info to it.
http://localhost:9200/postcode/_search?routing=AU&pretty=true&q=country:AU
I thought by doing this it would force the query into giving me just the AU place names, but this time it gave me only 3936 results
So I Am not quite sure what I have done wrong, the examples I have read show the queries changing from needing a filter, to just using match_all{} which I would have thought would only being back ones matching the au country code.
Thanks for your help in getting this to work correctly.
Almost have this working, it now gives me the correct number of results in a single shard, however the create index is not working quite right, it ignores my number_of_shards setting, and possibly other ones too
$index = $client->getIndex($indexname);
$index->create(array('mappings'=>array("$indexname"=>array("_routing"=>array("required"=>true))),'number_of_shards' => 6,
'number_of_replicas' => 1,
'analysis' => array(
'analyzer' => array(
'indexAnalyzer' => array(
'type' => 'keyword',
'tokenizer' => 'nGram',
'filter' => array('shingle')
),
'searchAnalyzer' => array(
'type' => 'keyword',
'tokenizer' => 'nGram',
'filter' => array('shingle')
)
)
) ), true);
I can at least help you with more info on where to look:
http://localhost:9200/postcode/_search?routing=au&pretty=true
That query does indeed translate into "give me all documents on the shard where documents for country:AU should be sent."
Routing is just that, routing ... it doesn't filter your results for you.
Also i noticed you're mixing your "au"s and your "AU"s .. that might mix things up too.
You should try setting required on your routing element to true, to make sure that your documents are actually stored with routing information when being indexed.
Actually to make sure your documents are indexed with proper routing explicitly set the route to lowercase(countrycode) when indexing documents. See if that helps any.
For more information try reading this blog post:
http://www.elasticsearch.org/blog/customizing-your-document-routing/
Hope this helps :)

CakePHP pagination on custom route

I obviously have a fundamental misunderstanding of how pagination works in CakePHP.
I have the following route set up which shows all posts in a category:
Router::connect('/:parent/:category',
array('controller' => 'posts', 'action' => 'viewCategory'),
array('parent' => '[a-z0-9-]+', 'category' => '[a-z0-9-]+'));
The pages work fine, however the pagination helper is outputting the wrong links for pagination.
I'm using $this->Paginator->numbers().
It's outputting links in this format: mysite.com/posts/viewCategory?page=2
rather than like this: mysite.com/parent-category/sub-category?page=2.
I've tried adding the following route after the first one and it still doesn't work:
Router::connect('/:parent/:category/:page',
array('controller' => 'posts', 'action' => 'viewCategory'),
array('parent' => '[a-z0-9-]+',
'category' => '[a-z0-9-]+',
'page' => '[0-9]+'));
For reference, my pagination options set in my view are as so:
<?php $this->Paginator->options(
array('url' =>
array('controller' => 'posts', 'action' => 'viewCategory')
)); ?>
What am I doing wrong here?
You are setting the url yourself
This is your paginator options call:
<?php
$this->Paginator->options(array(
'url' => array(
'controller' => 'posts',
'action' => 'viewCategory'
)
));
?>
Where you are overriding the current url - and explicitly requesting that the paginator uses the the '/posts/viewCategory' url (with no arguments) as it's base url.
Just don't define the url
Simply don't call options and the helper will use the current url - that should mean that if the current url is:
/parent-category/sub-category
Then page 2 will be (assuming you are using the paramType option to use GET arguments rather than named parameters):
/parent-category/sub-category?page=2
If that's not the case there's information missing from the question; it's important to distinguish between "vanity routes not being used" and "the url is not equivalent (the current situation).
Just had a battle fixing something similar and came across this post. Though old, but I think my answer might save someone the time I had to spend fixing it.
Basically, what you need to do is call the Paginator->options() before Paginator->numbers(), thus:
$this->Paginator->options(
array(
'controller' => 'parent-category',
'action' => 'sub-category'
)
);
Though the controller and action do not exist, it just tricks CakePHP to use them "AS IS", since the reverse routing isn't working!
And for those (like me), who want have set up a route similar to
Router::connect(
'/go/page:id',
array(
'controller' => 'blog',
'action' => 'paginated'
)
);
There might be difficulty setting up the Paginator options. This, however, worked for me:
$this->Paginator->options(
array(
'controller' => 'go',
'action' => '/'
)
);
I guess you know why it worked ;)

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 - Inflection Configuration

I have a table product_media and cake is not inflecting media to medium.
I have added the following to app/Config/bootstrap.php:
Inflector::rules('singular', array('irregular' => array('media' => 'medium')));
This works fine for a table named media but does not work for my table named product_media.
Anyone know why?
It probably matches the whole word only if no specific rules are given. Try this:
Inflector::rules(
'singular',
array(
'rules' => array(
'/(.*)media$/i' => '\1medium'
)
)
);
Inflector::rules('uninflected', array('media', 'medium'));
or
Inflector::rules('uninflected' => array('singulars'),
'irregular' => array('media' => 'medium')

Categories