CakePHP - How can i define route to a non-cakephp file that doesnt have any controllers associated with it?
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 controlle开发者_如何学运维rs associated with it?
I have placed this file(sitemap.php) in webroot folder, for my convenience. Now i need to route to it somehow!
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!
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.
精彩评论