How to route user generated paths in cakephp eg mysite.com/username1? - cakephp

I'm making a cakephp 2.3 app and have these "pages":
mysite.com/demo
mysite.com/admin
The above are fixed. The below are generated:
mysite.com/johnsmith
mysite.com/cooluser88
I've updated the routes with:
Router::connect(
'/*/',
array('controller' => 'pages', 'action' => 'redirect')
);
Am I on the right track? a redirect method in the pages controller? I'm using the official docs but I haven't been able to find specific examples.
I'll keep working on it and will update with an answer if I find one!

Your best bet IMO is to do something like reddit where each slug is prefixed with /r/.
Example:
mysite.com/u/johnsmith
Then, in your routes, you can do something like this:
Router::connect('/u/:uname/*', array('controller'=>'users', 'action'=>'display'),
array('pass'=>array('uname')));
If you really don't want to do that, you can use something like:
Router::connect('/:uname/*', array('controller'=>'users', 'action'=>'display'),
array('pass'=>array('uname')));
But keep in mind, any other controllers, plugins...etc etc etc will need their own routes, since it will think that ANYTHING passed after the mysite.com/ is a uname. Not the best idea IMO.

Related

Hiding the controller path in the URL

I have these URLs:
https://my.example.com/api/foo
https://my.example.com/api/bar
https://my.example.com/api/baz
In order to simplify it for the user and also to emphasise the different meaning I have set up these subdomains which have the same DocumentRoot as https://my.example.com/:
https://foo.example.com/
https://bar.example.com/
https://baz.example.com/
Due to the fact I am not capable to do the final step which means the URLs are still like this:
https://foo.example.com/api/foo
https://bar.example.com/api/bar
https://baz.example.com/api/baz
which isn't really simplifying it as the user has still to append api/<name> to the URL.
My question:
Is there a way to omit /api/<name> and redirect it somehow in the CakePHP controller so it's enough to enter https://foo.example.com/ and this would run the code in https://foo.example.com/api/foo?
I can check the current subdomain using $_SERVER ["SERVERNAME"] so this isn't the problem.
What makes it a bit more difficult:
The login page is as usually accessed through https://foo.example.com/users/login which means when /users/login is a part of the URL then the invisible redirect must NOT be triggered. However, I think I can prevent this checking the content of the $_SERVER variable.
I really hope you guys understand what I mean.
PS: I don't want to use Apache's redirect as this would display the final URL https://foo.example.com/api/foo in the browser.
Running CakePHP 4.1.4
I don't really see how this is more simple, if anything I'd find it to be less verbose, which is never good for a URL. Anyways, it sounds like you're looking for host bound routes, ie the _host option.
This would connect / to foo.example.com, bar.example.com, and baz.example.com, each to a different controller depending on the host:
$routes->scope(
'/',
function (\Cake\Routing\RouteBuilder $builder) {
$builder
->connect('/', ['controller' => 'Foo'])
->setHost('foo.example.com');
$builder
->connect('/', ['controller' => 'Bar'])
->setHost('bar.example.com');
$builder
->connect('/', ['controller' => 'Baz'])
->setHost('baz.example.com');
$builder
->connect('/users/login', ['controller' => 'Users', 'action' => 'login']);
}
);
The /users/login URL would connect to any host, as it's not being restricted, ie all of these URLs would work and point to the same controller and action:
foo.example.com/users/login
bar.example.com/users/login
baz.example.com/users/login
That could possibly introduce further problems, like for example cookies not working if they're not scoped for all subdomains, or duplicate content as far as SEO goes. So make sure that you keep your routes as specific as possible!
Also note that resource routing won't work properly with this, as it requires a path element, ie you cannot connect resources on just /, eg foo.example.com, it would have to be foo.example.com/resource-name, if you'd wanted to connect to the former, each resource route would need to be connected manually.
Finally, if you're not using named routes, then you'll have to use '_host' => 'foo.example.com' in your URL arrays if you want to generate URLs!
See also
Cookbook > Routing > Matching Specific Hostnames

changing defaultroute in cakephp

I am very new to cakephp.
I have my project 'registration' in the workspace. I have created an IndexController, which contains method index().
When I run my project by using workspace/registration/ it displays the following error:
Error: WorkspaceController could not be found. Create the class WorkspaceController below in file: app/Controller/WorkspaceController.php.
I want to exicute IndexController first. Somebody advised me to change the default route. But I dont know how to change the route. Please help me.
CakePHP has a fantastic documentation -> cookbook
And to your question, did you try to do what they suggested? Creating the class WorkspaceController in Controller!
In app/Config/routes.php:
Router::connect(
'/workspace/registration',
array('controller' => 'index', 'action' => 'index')
);
If /workspace/registration is where you register to join a workspace, though, I'd really recommend creating a WorkspaceController to deal with all workspace-related functionality.
Work with the default routes, not against them, it will make your life a lot easier.

dynamic html redirects using cakephp routers

dynamic html redirects  using routers
in the beginning I had no categories and all of my pages were root
example:
http://domain/somepage
This was great, but as my content over the years grew I need to categorize my content
so I added some routes see below
//routes.php
Router::connect(
"/:category/:slug",
array('controller' => 'controllername', 'action' => 'view'),
array(
'name'=>'[-A-Z0-9]+',
'pass' => array('category','slug')
)
);
//end
this works great and accomplished what I needed to do, but there is one problem the search engines .I need to write 301's for all of my links and I have over 8K pages.
The solution cakesphp's Router::redirect
The issue I am now having is I cant figurer out how to redirect my old links. I can for example redirect all of the links to one category, but that wont cut it. I need to redirect all of my links to the new location.
I am trying to use routes.php router :: redirect
if I do this my code it redirects to the category, but not the slug
Router::redirect(
'/:slug/*',
array(
'pass' => array('category/:slug'))
result
http://domain/category/
how can I get cake to redirect to
http://domain/category/slug ?
instead of http://domain/category/
I had all of my links pointing to the root directory
http://domain/somepage
http://domain/anotherpage
http://domain/ect
I needed to add categories
such as
`
http://domain.com/phones/samsung.php
http://domain.com/books/cakephp.php
`
I didn’t want to use .htaccees file because
My hosing provide limits me to 100 redirects
and i have over 8K links i need to redirect
I am trying to use cakes router ::redirect function in the routes.php file.
the below code works only for one category it doesn’t do it dynamically like I would like it too.
I tried to create a router class that would do this for me like you suggested, but to be honest with you I am not an expert in cakephp. Its easy to learn and a great framework I just dont know how to make my own classes or components yet. I just haven’t found good documentation to do this yet.
//routes.php
$move='category/'. stripslashes_deep ($_SERVER['REQUEST_URI']); Router::redirect('/:slug/*',$move, array('status' => 302)); code
Your best bet is to use a custom route class.
Custom routing extends the CakeRoute class and will allow you to return/add/modify parameters that are passed over.
CakePHP Custom Route Classes - How to Pass Arguments?

Configuring CakePHP 2.0 routing for domain/parameter (without controller name)

I would like to be able to route something like the following in CakePHP 2.0:
domain.com/london
domain.com/milton keynes
to a specific controller and action.
The application has multiple controllers so it should only use this route if the parameter provided doesn't match a controller name.
I achieved this with CakePHP 1.3.12 by adding the following code to the bottom of config/routes.php
Router::connect(
'/:location',
array('controller' => 'articles', 'action' => 'testing'),
array('pass' => array('location'), 'location' => '[a-z ]+')
);
Using this code with CakePHP 2.0 only works if I comment out the require line from config/routes.php, but then I loose the default routes so that a URL pointing at any other controller is caught by this.
How can I achieve the desired routing?
As far as I know this shouldn't work in Cake 1.3 either, simply because your [a-z ]+ regex also matches the case for a simple /controller_name route; the Router has no way to distinguish between the two and will thus always route to the one it encounters first.
You can, however, create a custom route class to achieve this. Mark Story (one of the Cake devs) wrote an excellent post about it quite some time ago, it's for Cake 1.3 but you can easily apply the principle to 2.0 (I know 'cause I'm using it in my 2.0 app). You can the find the post here.
This might not answer the specific question asked above, but it's relevant, this page comes up in search results, and my goal is to save whoever finds it (including future me I guess) some time on not having to research what I just researched.
I needed to add routing with a parameter for a different domain. For example, example.com should behave as usual, while example.org/some_page should route directly to a specific controller and action. So I've added the following to my Config/routes.php:
if ( CakeRequest::host()=='example.org' ) {
Router::connect('/:my_variable',
array(
'controller'=>'my_controller',
'action'=>'my_action'
),
array(
'my_variable'=>'[a-zA-Z-0-9 ]+',
'pass'=>array('my_variable')
)
);
}

Use of Routing in CakePHP

Why we are using routing in cakePHP and what would be the basic approaches for implementation...?
Routing allows aliasing and routing(!) of URLs. It gives us a cleaner, more controlled interface and underpins the functioning of CakePHP.
The first step would be to read the appropriate chapter in the book: http://book.cakephp.org/view/542/Defining-Routes (1.2) or http://book.cakephp.org/view/948/Defining-Routes (1.3)
Then look at the routes.php file (app/config/routes.php) to understand how it goes together.
Finally, when you know what you want to do (we don't because you haven't told us), try it debug it and use it.
Why
Because it allows you to decouple your URLs from your controller actions. You can name your controllers and actions in a way that makes sense internally, and invoke them using URLs that do not need to bear any resemblance to your internal naming scheme.
FooApiVersion1Controller::internal_beta_method() can be invoked by the URL /api/v1/method, and you can swap out the controller or method at any time without needing to change the URL.
How
Read the manual. http://book.cakephp.org/view/945/Routes-Configuration
http://book.cakephp.org/view/310/Configuration
<?php
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
?>

Resources