I am using cake php and due to some reason i want to hide controller and action name from the url . current url us like
http://192.168.1.31/home/this_is_test
where home is controller name and this_is_test is slug which is dynamic . i want the url like
http://192.168.1.31/this_is_test.
my routes.php is
Router::connect('/', array('controller' => 'home', 'action' => 'index'));
Router::connect('/dashboard', array('controller' => 'dashboard', 'action' => 'index'));
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/admin/login', array('controller' => 'users', 'action' => 'login', 'admin' => true));
Router::connect('/contents/*', array('controller' => 'contents', 'action' => 'view'));
Router::connect('/home/*', array('controller' => 'Home', 'action' => 'index'));
I have read a couple of solution after googling . also tried this in routes.php . but no luck
Router::connect(
'/:query',
array('controller' => 'Home', 'action' => 'index',1),
array('query' => '[a-zA-Z]+')
);
anybody have idea about this if it is possible??
Your solution
For static text try this:
Router::connect('/this_is_test', array(
'controller' => 'home',
'action' => 'this_is_test OR any_other action name'
));
If it's dynamic
Router::connect('/:id',
array('controller' => 'home', 'action' => 'index'),
array(
'pass' => array('id'),
array('id' => '[A-Za-z]')
)
);
References: Cakephp2.x Route
I hope I knew what you really want to achieve. You can place the Route in the last position. Here is the reference .
Other option would be to use alias for your controller. So you call your controller some thing else and set a new name for your controller then call it in you Route.
If this doesn't work then you would need to write a bespoke Component in order to help you to do that.
Related
I know this question is been asked several times, and I went through all other solutions, but I could do not solve the issue.
https://www.example.com/cars/view/161/BMW-X5
to be
https://www.example.com/cars/161/BMW-X5
my routes.php
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/admin',array('controller' => 'users', 'action' => 'login', 'admin' => true));
CakePlugin::routes();
require CAKE . 'Config' . DS . 'routes.php';
You can simply connect with
Router::connect('/cars/*', array('controller' => 'cars', 'action' => 'view'));
CarsController::view() will receive single argument, which should be used to find car by name.
Link with $this->Html->link('My Link', array('controller' => 'cars', 'action' => 'view', $slug));
For more route components, you need to use named parameters:
Router::connect('/cars/:id/:slug',
['controller' => 'cars', 'action' => 'view'],
[
// :id parameter will be passed as argument of CarsController::view()
'pass' => ['id']
]
);
Link with $this->Html->link('My Link', array('controller' => 'cars', 'action' => 'view', 'id' => $id, 'slug' => $slug));
I have two function "product" and "view" in cakephp. If any one type domainname.com/item1 then call "product" function and if domainname.com/item1/item2 then call "view" function .
item1 and item2 is dynamic content.
Router::connect('/:category', array('controller' => 'Posts', 'action' => 'product'));
Router::connect('/:category/:title', array('controller' => 'Posts', 'action' => 'view'));
I use this code in routes.php
Problem is this if I enter domainname.com/item1 then it is call view function.
please suggest me how to use url rewriting in cakephp .
try to add /posts and specifie the order like this:
Router::connect('/posts/:category/:title',
array('controller' => 'Posts', 'action' => 'view'),
array(
// order matters since this will simply map ":category" to $category in your action
'pass' => array('category', 'title')
)
);
You can take a look to the doc http://book.cakephp.org/2.0/fr/development/routing.html
try this
Router::connect('/:category/:product',
array('controller' => 'posts', 'action' => 'view'),
array('pass' =>
array('product')
));
Router::connect('/:product', array('controller' => 'posts', 'action' => 'product'));
I've recently started a new job and I noticed that a page linked to in one of our mailing lists no longer exists. I'm a developer / sysadmin so I thought it would be simple to set up a redirect, however our site is running CakePHP on the backend which I've got no experience with.
I want to redirect oursite.com/page/online-store to just oursite.com
I've tried Router::redirect('/page/online-store', 'http://www.oursite.com', array('status' => 301)); at the bottom of that file but it doesn't seem to be working, it just hangs at page load until I comment that line out.
Relevant lines in routes.php:
// login and register
Router::connect('/login', array('controller' => 'accounts', 'action' => 'login'));
Router::connect('/register', array('controller' => 'accounts', 'action' => 'register'));
// home
Router::connect('/', array('controller' => 'site', 'action' => 'home'));
Router::connect('/admin', array('controller' => 'users', 'action' => 'login', 'admin' => true));
// dashboard
Router::connect('/admin/dashboard', array('controller' => 'pages', 'action' => 'dashboard', 'admin' => true));
// search
Router::connect('/search', array('controller' => 'products', 'action' => 'search'));
// pages
Router::connect('/page/site-map', array('controller' => 'site', 'action' => 'map'));
Router::connect('/page/home', array('controller' => 'site', 'action' => 'home'));
Router::connect('/page/*', array('controller' => 'pages', 'action' => 'index'));
I'd appreciate any help.
I can somehow change the generated and accepted routes for simple urls, in the routes.php:
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/logout', array('controller' => 'users', 'action' => 'logout'));
Router::connect('/register', array('controller' => 'users', 'action' => 'add'));
This works like a charm. However, this doesn't:
Router::connect('/eintrag/:id', array('controller' => 'entries', 'action' => 'view'));
Router::connect('/bearbeiten/:id', array('controller' => 'entries', 'action' => 'edit'));
When I try to get a route for this, via echo $this->Html->url(array('controller' => 'entries', 'action' => 'view', $entry['id'])), I get /entries/view/1. And the url /eintrag/1 is not accepted by the router.
How can I prettify my view and edit routes like I can do with parameterless routes?
You need to use a third param in your route, as you are passing it :id specifically.
// SomeController.php
public function view($id = null) {
// some code here...
}
// routes.php
Router::connect(
'/eintrag/:id', // e.g. /eintrag/1
array('controller' => 'entries', 'action' => 'view'),
array(
// this will map ":id" to $id in your action
'pass' => array('id'),
'id' => '[0-9]+'
)
);
should do it.
More info # the cookbook
$this->Html->url() is just a Helper Function, that simply generates a URL according to paramaters passed, however when you actually open this Url then it will route request made for /eintrag/1 to /entries/view/1
I've got a controller called Items and another one called Categories. Items may belong to different Categories.
The routes that I have are
/items
/items/:item_id
/items/categories
/items/categories/:category_id
And this is my routes.php
Router::connect('/items', array('controller' => 'items', 'action' => 'index');
Router::connect('/items/:item_id', array('controller' => 'items', 'action' => 'view'), array('pass' => array('item_id')), array("item_id" => "[0-9]+"));
Router::connect('/items/categories', array('controller' => 'categories', 'action' => 'index'));
Router::connect('/items/categories/:category_id', array('controller' => 'categories', 'action' => 'view', array('pass' => array('category_id')), array("category_id" => "[0-9]+"));
The problem is that the third route fails as it routes to the Items controller which it shouldn't.
How can I fix this problem?
be careful on the order of your routes... cake will match to the first occurrence, so try using your routes like this:
Router::connect('/items', array('controller' => 'items', 'action' => 'index');
Router::connect('/items/categories', array('controller' => 'categories', 'action' => 'index'));
Router::connect('/items/categories/:category_id', array('controller' => 'categories', 'action' => 'view', array('pass' => array('category_id')), array("category_id" => "[0-9]+"));
Router::connect('/items/:item_id', array('controller' => 'items', 'action' => 'view'), array('pass' => array('item_id')), array("item_id" => "[0-9]+"));
good luck
in you config/routes.php
put a line
Router::connect('/items/categories', array('controller' => 'categories',
'action' => 'index'));
or something similar.
it will redirect all you /items/categories to the index action of categories controller.
for admin pages (actions that use admin prefix):
Router::connect('/items/categories', array('controller' => 'categories',
'action' => 'index','admin'=>true));