Route to absolute URL CakePHP 2.0 - cakephp-2.0

I want to access my sitemap.xml file in /mywebsite/sitemaps/xml/sitemap.xml
Reading some documentation, it is explained that I can reach the absolute URL by using:
Router::url('/',true)
For some reason it doesn't work. How to solve this?

If your site map xml is generated outside of CakePHP, then you can create the folders and file in /mywebsite/app/webroot/sitemaps/xml/sitemap.xml and avoid routing altogether.
If you are dynamically generating the xml through cake, then you don't want to hard-code the route. Rather, you would route to the controller and action that provide the xml return. A very basic example would be..
Router::connect(
'/sitemaps/xml/sitemap.xml',
array(
'controller' => 'Xml',
'action' => 'sitemap'
)
);
The XML documentation here can help you get started building and routing the site map.
http://book.cakephp.org/2.0/en/core-utility-libraries/xml.html

Related

CakePHP URLs having .html extension

We are converting an exisiting HTML site into a CMS using CakePHP. Since SEO of the site has been mapped with keywords and indexed by Google the static pages i want to have urls to have the extention .html
I had a look at the Document here
But am not quite sure how to achieve this in the right way.
Any one who has worked on it can give some pointers?
simply put this line into your Router Router::parseExtensions('html');
This will tell the Router to cut off the .html as an Extension and parse what remains.
To Create correct Links to the Pages you have to give the Link() function another Parameter called "ext".
Like this:
$this->Html->link(
'Super Seo link',
array(
'controller' => 'anyController',
'action' => 'someAction',
'title' => 'seo-title-for-gods-sake',
'ext' => 'html'
) );
Have fun! Florian

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

cakephp plugin to make URL SEO friendly

How do I make cakephp website URL SEO friendly? Is there a plugin available for cakephp 1.2.6
First define what you mean by "SEO friendly".
Most likely this just means you want to add the name of your "object" to the URL, i.e. add a slug. You can do that without any plugin:
echo $html->link($record['Model']['name'], array(
'controller' => 'foo',
'action' => 'bar',
$record['Model']['id'],
Inflector::slug($record['Model']['name'])
));
// -> /foo/bar/42/the-name
If you require any more customized URLs, first specify what exactly you need, then use Routing to create these custom URLs.
it's the only way to do it wothut any trouble...
but if you don't like id in the url, then you should take a title, remove all symbols, replace spaces with -, and write to the datebase...

Resources