【问题标题】:Create plugin cakephp and use it in browser创建插件 cakephp 并在浏览器中使用
【发布时间】:2015-06-15 17:18:05
【问题描述】:

我正在尝试创建一个plugin,这是我的文件夹布局;

在 bootstrap.php 中

CakePlugin::load('ContactManager');

在 routes.php 中

Router::connect(
    '/ct/', 
    array(
        'plugin' => 'ContactManager', 
        'controller' => 'Contacts', 
        'action' =>'index'
    )
);

ContactManagerAppController.php

<?php
class ContactManagerAppController extends AppController {}

ContactsController.php

<?php
class ContactsController extends ContactManagerAppController {
    public $uses = array('ContactManager.Contact');

    public function index() {
        //...
    }
}

ContactManagerAppModel.php

<?php
class ContactManagerAppModel extends AppModel {}

联系方式.php

class Contact extends ContactManagerAppModel {}

如何在浏览器中显示 index.ctp index.ctp

<?php echo 'hello'; ?>

请求http://localhost/cakephp/ct/ContactManager/Contacts 给出了缺少控制器错误,而不是我的插件控制器索引:

【问题讨论】:

  • 你的截图仍然是http://localhost/cakephp/ContactsManager/Contacts。由于您创建了一个路由,以便 / 指向您的插件,因此 url http://localhost/cakephp 将指向该控制器操作 如果它没有 意味着更基本的事情是错误的,比如查看错误的应用程序/编辑错误的文件插件正在加载等
  • Router::connect('/ct/', array('plugin' => 'ContactManager', 'controller' => 'Contacts', 'action' =>'index'));和我的链接:localhost/cakephp/ct/ContactManager/Contacts 它不起作用,:(
  • 嗯,这并不奇怪,因为这与您评论中的路线不匹配,也不匹配任何其他路线。这只会导致“缺少 ct 控制器”错误。我推荐你get on irc,因为看起来你有一些误解需要澄清。
  • 您已经有了,您的问题不在于创建插件,而在于其他东西。 SO 是这种帮助的错误格式。祝你好运。

标签: cakephp cakephp-2.0


【解决方案1】:

您应该能够从/contact_manager/contacts 访问您插件的联系人控制器(无需设置路由)。因此,从您的问题来看,绝对 URL 应该是:-

http://localhost/cakephp/contact_manager/contacts

如果这不起作用,则插件设置有问题。确保插件文件是可读的并且文件名和类都是正确的。否则,您的插件结构看起来不错。

如果上述 URL 有效,那么(并且只有那时)您可以考虑重新路由它。为此,您需要执行以下操作(最好在 /app/Config/routes.php 中):-

Router::connect(
    '/ct/', 
    array(
        'plugin' => 'contact_manager', 
        'controller' => 'contacts', 
        'action' =>'index'
    )
);

如果路由不起作用,请尝试检查路由文件中其他地方是否没有覆盖它。

您应该在路由参数中使用蛇形大小写(例如“contact_manager”)而不是驼形大小写(例如“ContactManager”)。

【讨论】:

  • 谢谢你!我知道我在缺少文件夹配置(bootstrap.php 和 routes.php)i.imgur.com/Ju8mq4D.jpg 的文件夹中出错了
  • @Scyllar01 这解决了您的问题吗?如果是这样,请接受答案,以便其他找到此页面的人也可以解决他们的问题。 :-)
【解决方案2】:

这是我的回答 ---> 谢谢大家 http://i.imgur.com/Ju8mq4D.jpg

在控制器中:

AppController.php
ContactsController.php

在模型中

AppModel.php
Contact.php

在视图/联系人中

index.ctp

app/Config/routes.php

Router::connect('/ct', array('plugin'=>'ContactManager', 'controller' => 'contacts', 'action' => 'index'));

app/Config/bootstrap.php

CakePlugin::load(array('ContactManager' => array('bootstrap' => true, 'routes' => true) ));

【讨论】:

  • 这看起来不正确。您是在暗示您在插件中创建了 AppController 和 AppModel?
猜你喜欢
  • 2010-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多