简体   繁体   English

CakePHP - 如何定义到没有与之关联的任何控制器的非cakephp文件的路由?

[英]CakePHP - How can i define route to a non-cakephp file that doesnt have any controllers associated with it?

Im working in CakePHP now. 我现在在CakePHP工作。 I'd like to know how i can define a route to a non-cakephp file that doesnt have any controllers associated with it? 我想知道如何定义一个非cakephp文件的路由,该文件没有任何与之关联的控制器?

I have placed this file(sitemap.php) in webroot folder, for my convenience. 为方便起见,我已将此文件(sitemap.php)放在webroot文件夹中。 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. 听起来您希望能够在cakephp应用程序中使用sitemap.php中的功能。 The bet way to include this in cakephp is by setting it up as a vendor. 将其包含在cakephp中的赌注方式是将其设置为供应商。 Follow these steps: 跟着这些步骤:

1- Put the file in the app/vendor folder. 1-将文件放在app / vendor文件夹中。 2- To use the file in a controller (or anywhere else), add: 2-要在控制器(或其他任何地方)中使用该文件,请添加:

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. 如果它只是一个包含函数列表的文件,您现在可以像在任何其他PHP文件中一样调用函数。 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()的函数,则在导入供应商/站点地图的控制器中,您只需输入:

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: 因此,现在您已准备好设置路由以在config / routes.php文件中包含站点地图功能:

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. 这将处理操作中包含的代码序列,然后播放sitemap.php文件。

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: 在config / routes.php中你会添加:

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 有关路由的更多信息,请访问: 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. 如果你没有真正使用它,我会跳过整个CakePHP过程。 Unless your .htaccess is overly greedy (rewriting requests to file that * exist*), you should be able to access sitemap.php directly. 除非您的.htaccess过于贪婪(重写请求*存在*存在*),否则您应该能够直接访问sitemap.php If you can't, update the .htaccess file to not rewrite existing files. 如果不能,请更新.htaccess文件以不重写现有文件。

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?). 现在,如果外部服务需要文件为`sitemap.xml',请不要尝试在CakePHP中进行重写,只需用.htaccess文件重写(通过你的评论,或许你正在做什么?)。

The bottom line: Don't rewrite unless you have to, and don't rewrite with CakePHP if you're not even using it. 底线: 除非必须,否则不要重写,如果你甚至不使用CakePHP,也不要重写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM