My current url is
baseUrl/categories/category/categoryName/subcatName...
To convert this url into
baseUrl/categoryName/subcatName...
i am using following route
Router::connect(
'/:slug/*',
array(
'controller' => 'Categories',
'action' => 'category'
),
array(
'pass' => array('slug'),
'slug'=>'[a-zA-Z]+'
)
);
but it is creating problem with urls like:
baseUrl/home
baseUrl/myaccount ..
etc.
and redirect them to category action.
The common solution to this problem is to add a single letter (like Reddit).
baseUrl/r/slug/something-awesome
That way, you can make the route:
Router::connect(
'/r/:slug/*',
array(
'controller' => 'Categories',
'action' => 'category'
),
array(
'pass' => array('slug'),
'slug'=>'[a-zA-Z]+'
)
);
Related
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
I'm trying to set up a CakePHP redirect route, but I'm stuck. Reading the book didn't help so far.
I want to redirect the URL /mysite/xyz to /mysite/en/xyz (which includes a language parameter)
The route for the final URL, /mysite/en/xyz, is set up like so, and works fine:
Router::connect(
'/:language/:category/*',
array('controller' => 'content', 'action' => 'index'),
array(
'named' => array('language', 'category'),
'language' => 'en|de',
'category' => 'abc|def|xyz',
'pass' => array('language', 'category')
)
);
The redirect route is set up like so:
Router::redirect(
'/:category/*',
array('controller' => 'content', 'action' => 'index', 'language' => 'en'),
array(
'named' => array('category'),
'category' => 'abc|def|xyz',
'pass' => array('language'),
'persist' => array('category')
)
);
However, if I now access the URL /mysite/xyz, I'm redirected to /mysite/mysite/en/xyz/en - note the duplication of parameters.
How can I set this up correctly?
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'
)
);
In my CakePHP App I'd like to pass arguments in a custom route.
What works now
(domain/controller/action/param)
domain.com/dealers/view/1
What I'd like to do
(domain/param/controller/action/param)
domain.com/washington/dealers/view/1
This is my code in routes.php:
Router::connect('/:city/dealers/view/:id', array('controller' => 'dealers', 'action' => 'view'),
array(
'pass' => array('city', 'id')
),
array('city' => '[a-z]+')
);
This just redirects domain.com/washington/dealers/view/1 to domain.com/dealers/index for the obvious reason that I did not properly pass the parameters. Does anyone know what I am missing?
city should not be in a separate array ex:
Router::connect(
'/:city/dealers/view/:id',
array('controller' => 'dealers', 'action' => 'index'),
array(
'pass' => array('city', 'id'),
'city' => '[a-z]+'
));
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.