cakephp 2.5 Router - part of language string in URL - cakephp

CakePHP Version 2.5.2
Config.Language 'spa'
If i'm in
mydomain.com/backoffice
and i reload (ctrl-r) i get again
mydomain.com/backoffice
If i call in UsersController in action deleteCache at the end:
return $this->redirect('/backoffice');
i get
mydomain.com/users/sbackoffice
Please note the "s" before backoffice. That seems to be a part of the config.language
If i set the Config.language to 'eng' i get
mydomain.com/users/ebackoffice
Please note now the 'e' before backoffice.
My routes.php:
Router::connect('/backoffice', array('controller' => 'users', 'action' => 'backoffice'));
Router::connect('/:language/:controller/:action/*',
array(),
array('language' => 'eng|spa')
);
Router::connect('/:language/:controller',
array('action' => 'index'),
array('language' => 'eng|spa')
);
Router::connect('/:language', array('controller' => 'technologies', 'action' => 'home'),
array('language' => 'eng|spa')
);
Any idea?
How can i debug this?

Related

CakePHP setflash page refresh doesn't keep language in url

I have a multi language website, but when I don't do a redirect with a setFlash, it deletes the language from the url. Example: if the contactform doesn't validate, it just does a setFlash without the redirect, so that user input data is not lost. But it does refresh the page and deletes the language part from my url. This does not happen when I do a redirect after my setFlash, because I give the language param with the redirect. Work flow:
I start on the page website.com/eng/contact (notice the language part). I fill in the contact form and fill in all required inputs. It redirects me to website.com/eng/contact/send. That's great and what I want. But when I don't give an valid email adres for example, it display an error (great), but the url has changed to website.com/forms/contact (notice the missing language part, and it doesn't use my routes). What am I doing wrong? My code:
Formscontroller.php
public function contact() {
if ($this->request->is('post')) {
$this->Form->set($this->request->data);
if ($this->Form->validates()) {
if($this->Form->save($this->request->data)){
$this->redirect(array('controller' => 'forms', 'action' => 'contact_success', 'language' => $this->Session->read('Config.language')));
} else {
$this->Session->setFlash(__('Er ging iets mis met het versturen van uw contactformulier, probeer het opnieuw.'), 'flash_error');
}
} else {
$this->Session->setFlash(__('Niet alle verplichte velden zijn ingevuld.'), 'flash_error');
}
}
}
routes.php
Router::connect('/:language/pages/*', array('controller' => 'pages', 'action' => 'display'));
Router::connect('/:language/contact', array('controller' => 'forms', 'action' => 'contact'), array('language' => 'ned|eng'));
Router::connect('/:language/contact/verzonden', array('controller' => 'forms', 'action' => 'contact_success'), array('language' => 'ned'));
Router::connect('/:language/contact/send', array('controller' => 'forms', 'action' => 'contact_success'), array('language' => 'eng'));
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/:language', array('controller' => 'pages', 'action' => 'display', 'home'), array('language' => 'eng|ned'));
Router::connect('/:language/:controller/:action/*', array(), array('language' => '[a-z]{3}'));
CakePlugin::routes();
require CAKE . 'Config' . DS . 'routes.php';
I was looking in the wrong direction for my problem. As ndm points out in his comment on my question, it had to do with the action of my form, it didn't have the language part in it. So thanks to ndm for pointing me in the right direction!

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

Cakephp rerouting

Hi I d'like to be able to rewrite my url. I have this format of url:
localhost/example/urlrewriting/Links/view/1
and I want to change this url to
http://localhost/example/urlrewriting/tt/my-link-1
In my Routes.php I have done this:
Router::connect('tt/:slug-:id',
array('controller' =>'links', 'action' => 'test'),
array('pass' => array('slug', 'id'), 'slug' => '[a-z0-9\-]+', 'id' => '[0-9]+'));
and in my controller I have this
public function test($param1, $param2){
debug($this->request->params);
}
I've always find this answer:
TtController could not be found.
cakephp 2.4.1
Please add / before the connect params like:-
Router::connect('/tt/:slug-:id',
array('controller' =>'links', 'action' => 'test'),
array('pass' => array('slug', 'id'), 'slug' => '[a-z0-9\-]+', 'id' => '[0-9]+'));

CakePHP routes with sub-folders

I have a problem with CakePHP route
Router::connect(
'/catalog/:slug/:slug2/*', array(
'controller'=>'pages',
'action'=>'view'
))
When I have url
/catalog/something/page:2 - it also catches this link. But it shouldn't, because there is no slash after params page:2, How to fix it? Thanks!!
I hope this may be helpful.
Router::connect(
'/catalog/:slug/:slug2/*', array(
'controller'=>'pages',
'action'=>'view'
), array('pass' => array('slug', 'slug2')));
and in your view file you can write like this to generate a link for above.
echo $this->Html->link('link', array(
'controller' => 'pages',
'action' => 'view',
'slug' => 'slug',
'slug2' => 'slug2'
));

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

Resources