CakePHP - How to use connectNamed right? - cakephp

I can't figure out how I can write this right to get 'cat' and 'city' as $this->params['named'] within my controller:
Router::connect('/this-is-a-simple-url', array('controller' => 'listings', 'action' => 'search', 'cat' => 200, 'city' => 57));
I tried to place Router::connectNamed(array('cat', 'city')); before that rule, but this is not changing anything.
Please help me out!
Thanks so much!

you mean like:
Router::connect('/this-is-a-simple-url',
array(
'controller' => 'listings',
'action' => 'search',
array(
'named' => array(
'cat' => '[a-Z]+',
'city' => '[\d]+'
)
)
)
);

Related

(issue) redirect route cakephp

sorry, my English so bad.
In CakePHP 2.x, I want to redirect old link to new link in my website. But i got some issue.
Example:
mywebsite.com/news/news redirect to mywebsite.com/news
I use:
Router::redirect ('/news/news',
array('controller' => 'categories',
'action' => 'index',
'slug' => 'news'
),
array(
'status' => '301'
)
);
Router::connect('/news', array(
'controller' => 'categories',
'action' => 'index',
'slug' => 'news'
));
=> OK, it work.
But
Example:
mywebsite.com/news/news/a-b-c redirect to mywebsite.com/news/a-b-c
I use:
Router::redirect('/news/news/:slug',
array(
'controller' => 'articles',
'action' => 'view',
'category' => 'news'
),
array(
'status' => '301'
)
);
Router::connect('/news/:slug',
array(
'controller' => 'articles',
'action' => 'view',
'category' => 'news'
)
);
=> result is: mywebsite.com/articles/view/category:news
Please help, thank you !
You are redirecting all requests from news/news/* to articles/view/category:*
Do you need the 301 redirect in there at all?
Router::connect('/news/news/:slug',
array(
'controller' => 'news',
'action' => 'slug'
)
);
But on this basis, the controller will expect a defined action for every type of a-b-c action that can be passed as a slug

How can I make a single routing rule that matches "archive", "archive/2013" and "archive/2013/02"?

I know that these three routes can be condensed to a single route where :year and :month are optional parameters, but everything I've tried so far simply redirects me to the index if the exact parameters aren't given. For instance, "www.domain.com/blog/archive/" matches the first rules, but "www.domain.com/blog/archive/2013" was falling through to the index rule until I added another specific rule to match it.
Router::connect("/$slug/archive", array(
'plugin' => 'blog',
'controller' => 'blog_posts',
'action' => 'archive'
)
);
Router::connect("/$slug/archive/:year", array(
'plugin' => 'blog',
'controller' => 'blog_posts',
'action' => 'archive'
),
array('pass' => array('year') )
);
Router::connect("/$slug/archive/:year/:month", array(
'plugin' => 'blog',
'controller' => 'blog_posts',
'action' => 'archive'
),
array('pass' => array('year', 'month') )
);
Router::connect("/$slug/*", array(
'plugin' => 'blog',
'controller' => 'blog_posts',
'action' => 'index'
));
To make a single routing rule use the following code that matches "archive", "archive/2013" and "archive/2013/02"
Router::connect("/$slug/archive/*", array(
'plugin' => 'blog',
'controller' => 'blog_posts',
'action' => 'archive'
)
);

Cakephp router::connect to forward named params

http://domain.com/foo/c:1
How can I write working router::connect to send such links to
controller=listings
action=search
named array(cat=1) the id I do not know, it's a dynamic url
Please take note, that in the url it is "c" but I need to forward it as "cat".
Thanks in advance!
You can specify the routing as follow in the Config/routes.php.
```
Router::connect(
'/:query/*', array('controller' => 'listings', 'action' => 'search'), array(
'params' => array('query', 'cat'),
'named' => array(
'query', 'cat'
)
)
);
```
and specify the link as
```
echo $this->Html->link('text goes here', array('controller' => 'listings', 'action' => 'search', 'query' => 'foo', 'cat' => 1));
```

add path of controller in form->create in Cakephp

firstCode
echo $this->Form->create('User', array(
'id' => 'form-login',
'inputDefaults' => array(
'label' => false,
'div' => false,
)
));
Second Code
echo $form->create('User', array('url' => array('controller' => 'users', 'action' => 'login')));
sorry to ask this silly question.i want to merge second code into firstone ..as they are working fine independently.. i have tried many times but failed
You're probably mixing up the arrays. Should all be in one big array (as the create() method only takes 2 arguments), like this:
echo $this->Form->create('User', array(
'url' => array('controller' => 'users', 'action' => 'login'),
'id' => 'form-login',
'inputDefaults' => array('label' => false, 'div' => false)
));

Cakephp routing: using dash had broke it

I have the following 2 routes.
Router::connect('/photo-gallery/:slug-:id',
array('controller' => 'company_categories', 'action' => 'photo_gallery'),
array(
'pass' => array('id'),
'id' => "[0-9]+"
)
);
Router::connect('/:slug-my-string-:id',
array('controller' => 'company_categories', 'action' => 'category_companies'),
array(
'id' => "[0-9]+",
'pass' => array('id')
)
);
While the first works, the second doesn't.
What am I doing wrong ?
Thank you!
Your second route doesn't work because the whole string ":slug-my-string" is parsed as a route element not just ":slug" as you expect. Reference.
The solution that I finally found and it works as expected is the following:
Router::connect('/:slug-:string-:id',
array('controller' => 'company_categories', 'action' => 'category_companies'),
array(
'id' => "[0-9]+",
'string' => "my-string"
'pass' => array('id')
)
);
This way I can handle all kind of static strings used in route url pattern and there is no problem anymore regarding the parsing of dashes ("-"), which I think it was the problem.

Resources