2013-10-13 12 views

回答

30

下面是代碼:

App::import('Controller', 'Products'); // mention at top 

// Instantiation // mention within cron function 
$Products = new ProductsController; 
// Call a method from 
$Products->ControllerFunction(); 

希望它可以幫助一些之一!

+7

這絕對不推薦!根據您的代碼的作用,使用[components](http://book.cakephp.org/2.0/en/controllers/components.html)(如@xialinZZZ已經提到的),庫或[models](http://取而代之的是book.cakephp.org/2.0/en/models.html)。 – ndm

+1

可能是,但至少我的工作沒有被卡住,似乎很簡單的方法解決快速解決方案尋找器 –

+0

「cron控制器」的整個概念*設計完全*有缺陷。您應該使用外殼,並且您的數據處理邏輯應該位於模型中,以便在需要時可以在控制器和外殼之間共享。我也投下了這個答案,因爲這是你能做的最糟糕的事情之一。 – burzum

5

在控制器操作中使用$this->requestAction();方法。這不是最推薦的模式,但它可能很有用,並且可以根據您的參數返回數據或呈現視圖。

+0

爲什麼不推薦? –

2

我引用了手冊來找到解決方案。

public function that_controller_function_you_are_writing() { 

    # this is cakes way of running required 
    App::import('Controller', 'Users'); 
    $UsersController = new UsersController; 

    # now you can reference your controller like any other PHP class 
    $UsersController->that_function_you_needed(); 
} 

這是鏈接: http://book.cakephp.org/2.0/en/core-utility-libraries/app.html

-1

試試這個

<?php echo $this->Html->link("Logout,".$user["username"], array('controller'=>'Users' ,'action'=>'logout'));?> 
+1

您可以請添加一些您認爲這應該起作用的描述。 – Daniel

+0

在回答之前請反覆閱讀問題。 – Developer

+0

不相關。請重新閱讀問題。 – Divyek

2

App::import('Controller', 'XXX');我沒有工作。

我使用的蛋糕3.0

一段時間我做了後,它的工作

功能的控制器要撥打:

public function validateSomething($var = null) 
    { 
     return ... 
    } 

在不同的控制器,你需要調用以前的函數來驗證一些東西:

public function index() 
    { 
     // load the model you need depending on the controller you need to use 
     $this->loadModel('User'); 

    // use this in case you have tu instantiate a new entity 
     $user = $this->User->newEntity(); 
     $user = $this->User->patchEntity($user, $this->request->data); 

    // using the controller on the fly, you could assign it to a var 
    // call the function you need 
     $result = (new UserController())->validateSomething($user); 

    // Test if result has something: 
     $this->Flash->success(__($result)); 
    }