CakePHP default Controller without changing URL - php

May be this question is already asked. If it is, please provide the link. I searched but not found what I actually wanted.
I know how to define default controller via Router::connect() but that redirects users to a different place. For example, if I do this for domain www.example.com :
Router::connect('/', array('controller' => 'users', 'action' => 'index'));
People coming to www.example.com will be redirected to www.example.com/users/ (see, both URLs aren't same). My question is, what should I do if I want to connect a controller without adding /:controller so that users coming to www.example.com will be in www.example.com but get contents of UsersController?

Try this on your routes.php after
Router::connect('/login', array('controller' => 'users', 'action' => 'login'))
Now your example.com/users/login should looks like example.com/login and address will work.

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 Router::url not working correctly

I'am using CakePHP 2.0 for a small admin section of a website.
If the user is logged in correctly, I redirect them to the admins dashboard.
I do this as following:
$this->redirect(Router::url(array('controller' => 'admin', 'action' => 'dashboard')));
The redirect is done correct, but for some reason the URL where it redirects to isn't correctly build. The URL is in the following structure (note the double [root] sections in the URL - this is what is wrong):
http://localhost/[root]/[root]/admin/dashboard
Off course their are errors shown because this controller / action doesn't exists. The URL should be off this form:
http://localhost/[root]/admin/dashboard
I can't seem to find what the exact problem is since cakePHP is not my core dessert, is there anyone that can point me in the right direction?
Thanks!
$this->redirect("YOUR URL");
example $this->redirect('/admins/dashboard');
This way you can redirect easily!
You can use the 'full_base' parameter in Router::url.
e.g.
$url = Router::url( array(
'controller' => 'posts',
'action' => 'view',
'id' => $post[id],
'full' => true
) );
This will give you a full base url. I found this fixed routing problems when I was running CakePHP in a subdirectory of localhost.
you can simply right like this
$this->redirect(array('controller' => 'admins', 'action' => 'dashboard'));
You can simply right this
Router::connect('/', array('controller' => 'users', 'action' => 'index'));

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)
);

Routing search engine friendly URLs for PagesController in CakePHP

I'm try to create search engine friendly URLs for the pages controller, i.e. /about instead of /pages/about.
I've tried setting up the following routes (at the bottom of routes.php):
Router::connect('/*', array('controller' => 'pages', 'action' => 'display'));
and
Router::connect('/:page', array('controller' => 'pages',
'action' => 'display'), array('pass' => array('page'), 'page' => '[a-z]+'));
Both properly match /about, /support, etc. However, failed when I had a action/method pair. For example, /contact should route to PagesController->contact(). However, the above routed it to PagesController->display().
There has to be a way to accomplish this without making a specific route for each page. How can I create a route or set of routes that:
Mimics the default route behavior for
the PagesController. That is routes
to display() unless a action/method
pair exists.
Does so with search engine friendly URL. That is coming from root / not /pages.
Demonstrate both the Router::connect() and Html->link()
I have checked for examples in the CakePHP Book and viewed other questions such as CakePHP routing in pages controller. Nothing seems to satisfy the specification above.
You need to create a second route for the contact method call and put it before the more generic rule to match "everything [a-z] after /pages". Try that before your rule:
Router::connect('/contact', array('controller' => 'pages', 'action' => 'contact'));
Always keep in mind that the order of routes is important. The more generic a rule is the more will it match. So put more specific rules in front of the more generic ones.

Remote controller name from CakePHP URL

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')

Categories