CakePHP - 如何定义到非cakephp文件的路由,没有任何控制器与它相关联? [英] CakePHP - How can i define route to a non-cakephp file that doesnt have any controllers associated with it?

查看:78
本文介绍了CakePHP - 如何定义到非cakephp文件的路由,没有任何控制器与它相关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在在CakePHP中工作。我想知道如何我可以定义一个非cakephp文件,没有任何控制器与其相关联的路由?



我放置了这个文件(网站地图。 php)在webroot文件夹中,为了我的方便。现在我需要路由到某种方式!

解决方案

听起来你想要能够使用sitemap.php的功能你的cakephp应用程序。在cakephp中包含这个的下注方式是通过设置它作为供应商。请按照下列步骤操作:



1-将文件放在app / vendor文件夹中。
2-要在控制器(或其他任何地方)中使用文件,请添加:

  App :: import 'vendor','sitemap'); 

如果它只是一个带有函数列表的文件,现在可以简单地调用函数将在任何其他PHP文件。因此,如果您有一个名为show_links()的函数,例如在导入供应商/网站地图的控制器中,您只需输入:

  show_links(); 

如果它是一个类,那么你将需要实例化类并使用它, else:

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

因此,现在您已准备好设置路由以将sitemap功能包含在config / routes .php文件:

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

这将处理动作中包含的代码序列,然后将从sitemap.php文件



简单来说,你会看到这样的:

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

在config / routes.php中,您将添加:

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

然后,当您访问网址:

  http://mysite/sitemap.xml 

将路由到:

  http:// mysite / site_maps / show_map 
pre>

有关路由的详细信息,请访问: http://book.cakephp.org/view/542/Defining-Routes



祝你好运和快乐编码!



UPDATED!


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!

解决方案

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!

这篇关于CakePHP - 如何定义到非cakephp文件的路由,没有任何控制器与它相关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆