dqmdlo9674 2013-04-14 16:17 采纳率: 100%
浏览 14
已采纳

cakephp 2.x Shell使用app Controller?

I want a shell script I've got to be able to access appController, is this possible, without copying that code into the AppShell ? If so any pointers ?

Thanks in advance

  • 写回答

1条回答 默认 最新

  • douhei8633 2013-04-14 16:54
    关注

    In general, shells shouldn't rely on code inside a controller, some pointers (as requested):

    • If the Shell has to perform data-related tasks, move the code to the Model. This is good practice in any case (look up 'Skinny Controllers, Fat Models' on Google)
    • Although not 'standard' supported, you can move the code to a 'Component' (see Components). Components are meant for re-usable portions of code used in Controllers.

    If the above options really aren't an option, you will have to manually initialise the AppController. keep in mind that, because you're running from the command line, various things will not be present, e.g. There will be no 'request' and some environment variables (e.g. host name) may not return the expected value!

    Manually initialising a Controller

    Manually initialising the controller will be something like this;

    App::uses('CakeRequest', 'Network');
    App::uses('CakeResponse', 'Network');
    App::uses('Controller', 'Controller');
    App::uses('AppController', 'Controller');
    
    // request/response may be optional, depends on your use
    $controller = new AppController(new CakeRequest(), new CakeResponse());
    $controller->constructClasses();
    $controller->startupProcess();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度