tags:

views:

295

answers:

2

Im working in CakePHP now. I'd like to know how i can define a route to a non-cakephp file that doesnt have any controllers associated with it?

I have placed this file(sitemap.php) in webroot folder, for my convenience. Now i need to route to it somehow!

+2  A: 

I'd skip the whole CakePHP process if you're not actually using it. Unless your .htaccess is overly greedy (rewriting requests to file that * exist*), you should be able to access sitemap.php directly. If you can't, update the .htaccess file to not rewrite existing files.

Now, if external services need the file to be `sitemap.xml', don't try to do the rewriting in CakePHP, just rewrite with the .htaccess file (which by your comments, perhaps you're doing?).

The bottom line: Don't rewrite unless you have to, and don't rewrite with CakePHP if you're not even using it.

Tim Lytle
Ok, I've managed to cod something like this in .htaccess file found in documentRoot: RewriteRule ^/sitemap.xml$ app/webroot/custom_scripts/sitemap.phpBut its not working, as it seems!
TomyLeeJhones
Wit hall due respect, why complicate the configuration when you can do this simply using cake? If you do not need to route to it through cake, putting it in the webroot will work just the same so long as the rewrite is enabled on apache and the cakephp instance is running using the .htaccess files that come with it. Just put the php file in the webroot directory and call it on the URL:http://mysite/sitemap.phpIt will work... again, so long as apache has rewrite enabled and the cakephp instance is using the .htaccess files it comes with.
cdburgess
After reading more information as it was posted, this seems to be the correct solution to the specified need. +1
cdburgess
@TomyLeeJhones - "But its not working, as it seems!" What is it doing instead of working? 500 error?
LeguRi
A: 

It sounds like you want to be able to use functionality from sitemap.php in your cakephp application. The bet way to include this in cakephp is by setting it up as a vendor. Follow these steps:

1- Put the file in the app/vendor folder. 2- To use the file in a controller (or anywhere else), add:

App::import('Vendor','sitemap');

If it is just a file with a list of functions, you can now simply call the functions as you would in any other PHP file. So if you have a function called show_links() for example, in the controller where you have imported the vendor/sitemap, you simply put:

show_links();

If it is a class, then you will need to instantiate the class and use it like you would anywhere else:

App::import('Vendor','sitemap');
$sitemap = new Sitemap;
$sitemap->show_links();

So, now you are ready to set up the route to include the sitemap functionality in the config/routes.php file:

Router::connect('/sitemap.xml', array('controller' => 'YOUR_CONTROLLER', 'action' => 'YOUR_ACTION'));

This will process the sequence of code contained in the action that will then play off the sitemap.php file.

So in a nutshell, you would see something like this:

<?php
class SiteMapController extends AppController
{
  var $name = 'Tests';
  function show_map()
  {
    App::import('Vendor','sitemap');
    $sitemap = new Sitemap;
    $sitemap->show_links();
  }
}
?>

And in the config/routes.php you would add:

Router::connect('/sitemap.xml', array('controller' => 'site_maps', 'action' => 'show_map'));

Then, when you visit the url:

http://mysite/sitemap.xml

It will route to:

http://mysite/site_maps/show_map

For more information on routing, you can visit: http://book.cakephp.org/view/542/Defining-Routes

Good luck and Happy Coding!

UPDATED!

cdburgess
@cdburgess.. I dont want this file(sitemap.php) to be associated with any of cakePHP's semantics. I just need to place it inside the app/webroot folder and be able to access it directly from www.domain/sitemap.xml
TomyLeeJhones
To my knowledge, cakePHP will not handle routing the way you are requesting. This will have to be done in Apache then. Time Lytle seems to have the correct solution if you are attempting to access the file in that manner. Apache will have to do the rewritting rules. CakePHP should be left out of the picture.
cdburgess
I have enabled the mod_rewrite in apache and i've coded:- RewriteRule ^/sitemap.xml$ app/webroot/custom_scripts/sitemap.php But its not working, as it seems! Is there any mistake in my code?
TomyLeeJhones
BTW... this solution will still work. All you need to do is put in the route as follows:Router::connect('/sitemap.xml', array('controller' => 'sitemap', 'action' => 'showmap'));
cdburgess
I have updated my answer. It will allow you to reference the sitemap.xml on the URL as you desire without having to modify any apache rewrite rules. The routing mechanism will work just fine through cake.
cdburgess
BUT im not using any controller here. Its just a stand alone plain PHP file.
TomyLeeJhones