Remote controller name from CakePHP URL - php

I have a problem with the url rewriting.
The problem that I am facing is that currently our urls are like this:
http://www.xyz.com/sc_users/index
I dont want the controller name to be shown in that url.
Is there a way to achieve that??
First of all thank your guys..
Like I have 8 controllers I dont want the controller name to be shown in my url....this is what I want..
To be more precise no controller name in my url

You can define custom routes in app/config/routes.php. You'll find the all about routes in the CakePHP cookbook under Defining Routes. For example, a custom route can look like this:
Router::connect(
'/the_url_you_want_to_use/*', array('controller' => 'sc_users', 'action' => 'index')
);

You need to read up about CakePHP routing, look at the examples under 'defining routes'. Update your question with what you would actually like your URLs to look like and we will be able to help you more effectively.

That's simple :
there is a file called routes.php in /config directory :
You can do url rewriting stuff there like this :
Router::connect('/pages/*', array('controller' => 'cmsPage', 'action' => 'render'));
You can pass more complicated variables to your controller :
Router::connect('/:id-:lang-:profile-:firstName-:lastName-:profile.htm',
array('controller' => 'profiles','action' => 'view'),
array('id'=>'[0-9]*', 'lang'=>'fr','firstName'=>'[^-]*','lastNAme'=>'[^-]*','profile' => $util->keywords['profiles'][0]['fr'], 'pass' => array('id', 'lang'),'profile' => $util->keywords2['profiles'][0]['en'])
)
;
As you can see in the last example I have passed 2 parameters to the controller through 'pass' => array('id', 'lang')

Related

Cakephp 3 - Routes being either an action or a parameter

I would like to know how to route the following scenario: I have a controller called Users, in this controller I have many actions, one of those is "profile".
I want my address being like that: mysite.com/users/NameOfTheUser OR mysite.com/users/edit-profile OR mysite.com/users/edit-photo, etc.
When you go for "edit-profile" you will be redirected to the edit_profile action, but here goes the trick, when you go to "NameOfTheUser" I want to redirect to the "profile" action, passing "NameOfTheUser" as a parameter.
Is there a way to do so without routing every action manually?
EDIT
I used the code that Yosi Azwan said, it works but I have to create a new route for each other page in the controller users.
Router::connect( '/users/:name', ['controller' => 'Users', 'action' => 'profile'], ['pass' => ['name']] );
Maybe this is what you are looking for
Router::connect( '/users/:name', ['controller' => 'Users', 'action' => 'profile'], ['pass' => ['name']] );
and read this for complete documentations http://book.cakephp.org/3.0/en/development/routing.html

Cakephp : What is wrong with my pagiantion and routing?

I am using cakephp 1.3.X . I am experiencing some problem with url routing and pagination
My URL will look like
http://project.dev/search/hamburg/place:1
Below is my Routing code
Router::connect('/search/:slug/*', array('controller' => 'searches', 'action' => 'index'));
Below is controller where I am coming to the search page
$this->redirect(array('controller'=>'searches','action'=>'index','slug'=>$slugUrl,$query));
My problem is when I go to the next page ,that won't showing the place:1 , just linking to search/hamburg/page:2 ie. place is missing
My index.ctp paginator options are given below
$paginator->options = array(
'url'=>array(
'controller'=>'searches',
'action'=>'index',
'slug'=>$this->params['slug'],
));
What going wrong with me. I've had searched a lot in cake articles , but nothing works for me. Please advise me
Add.
Router::connectNamed(array('slug'));
At the top of your router.php
Or define third pass param in your route, to pass slug.
Router::connect('/search/:slug/*', array('controller' => 'searches', 'action' => 'index'), array('pass'=>array('slug')));
Update view
$paginator->options = array('url' =>
array_merge(array('slug' => $this->params['slug']), $this->passedArgs)
);

How to create custom urls in cakephp

I have created a cakephp app. I have urls looks like
www.mysite.com/products/search/hardware
It loads fine. But I want urls that looks like
www.mysite.com/hardware
Can this is achieved by setting route connect
I appreciate any help.
Thanks.
Yes, you can use a route.
Router::connect(
'/hardware',
array('controller' => 'products', 'action' => 'search', 'hardware')
);
For a more general solution (any category name routing to products/search) see http://book.cakephp.org/view/948/Defining-Routes
You could add something like
Router::connect('/hardware',
array('controller' => 'products', 'action' => 'search'),
array('pass' => array('search'), 'search' => 'hardware'));
in your routes.php file, but then you would have to do it for each searchable item.
The problem you will be facing if you want something automatic, is that you need a way to differentiate your searchable products from every other model you have. So maybe you should settle for another type of URL like
www.mysite.com/products/hardware
or
www.mysite.com/s/hardware
and use the appropriate routes accordingly.
How about creating hardware_controller in your controllers folder.

Zend_Router omitting param-key

I've got a question considering Zend_Controller_Router. I'm using a a modular-structure in my application. The application is built upon Zend-Framework. The normal Routes are like this:
/modulename/actionname/
Since I always use an IndexController within my modules, it's not necessary to provide it in the url. Now I am able to append params like this:
/modulename/actionname/paramkey/paramvalue/paramkey/paramvalue
So this is normal in ZF, I guess. But in some cases I don't want to provide a paramkey within the url. For example I want a blog-title to be shown within the url. Of course this is intended for SEO:
/blog/show/id/6/this-is-the-blog-title
In this case, blog is the module, show is the action. id is a paramkey and 6 is the id of the blogpost I want to show. this-is-the-blog-title is of course the headline of the blogpost with the id 6. The problem is, that if I do use the assemble()-method of the router like this:
assemble(array('module' =>'blog',
'action' => 'show',
'id' => $row['blog_id'],
$row['blog_headline_de'] . '.html'));
the url results in:
blog/show/id/6/0/this-is-the-blog-title.html
As you can see a 0 is inserted as a key. But I want this 0 to be omitted. I tried this by using the blogtitle as key, like this:
assemble(array('module' =>'blog',
'action' => 'show',
'id' => $row['blog_id'],
$row['blog_headline_de'] . '.html' => ''));
This results in:
blog/show/id/6/this-is-the-blog-title.html/
Now the 0 is omitted, but I've got the slash at the end.
Do you have any solution to get an url without 0 as key and without an ending slash?
Regards,
Alex
You might want to use a custom route for this:
$router->addRoute(
'blogentry',
new Zend_Controller_Router_Route('blog/show/:id/:title',
array('controller' => 'index', 'module' => 'blog'
'action' => 'info'))
);
And call your assemble with the route as second parameter. See the Zend_Controller_Router_Route section of the documentation for more details (they even provide examples with assemble).
Or in a more general way:
$router->addRoute(
'generalseo',
new Zend_Controller_Router_Route(':module/:action/:id/:title',
array('controller' => 'index'))
);

Zend url view helper - keeping existing params in a reversed route

At the moment in my ZF project have a URL structure like this:
/news/index/news_page/1/blog_page/2
When I generate my pagination I use the URL helper as follows:
<?php echo $this->url(array('blog_page'=>3)); ?>
Which generates a URL like this:
/news/index/news_page/1/blog_page/3
What I'd like to do is use a custom route to have nicer URLs, something like this:
new Zend_Controller_Router_Route(
'news/:news_page/:blog_page',
array('controller' => 'news', 'action' => 'index')
);
However, when I try an use this route in the view helper:
<?php echo $this->url(array('blog_page'=>3), 'newsIndex'); ?>
It throws an error because I've not specified news_page in the params.
How can I get around this, and tell the url helper to use the 'current' values for these params?
The url helper will use the an existing parameter if it exists in the current request. It seems as if, in your particular case, the news_page param is not set in the request object. Setting a default value for the news_page parameter in your route should solve your problem.
So, your route definition should look something like this:
new Zend_Controller_Router_Route(
'news/:news_page/:blog_page',
array('controller' => 'news', 'action' => 'index', 'news_page' => 1)
);

Categories