Вопрос

How do I require_once in my controller, a third party class that I put in kohana\vendor\twitter-api-php? I tried this answer which was similar but I kept getting errors (see below). My project's file structure is as follows:

kohana
- application
- modules
- system
- vendor

I keep getting these errors

 Warning: Uncaught exception 'ErrorException' with message 'require_once(C:\wamp\www\kohana): failed to open stream: Permission denied' in C:\wamp\www\kohana\application\classes\Controller\Twitter.php:8 Stack trace: #0 C:\wamp\www\kohana\application\classes\Controller\Twitter.php(8): Kohana_Core::error_handler(2, 'require_once(C:...', 'C:\wamp\www\koh...', 8, Array) #1 C:\wamp\www\kohana\application\classes\Controller\Twitter.php(8): Controller_Twitter::before() #2 C:\wamp\www\kohana\system\classes\Kohana\Controller.php(69): Controller_Twitter->before() #3 [internal function]: Kohana_Controller->execute() #4 C:\wamp\www\kohana\system\classes\Kohana\Request\Client\Internal.php(97): ReflectionMethod->invoke(Object(Controller_Twitter)) #5 C:\wamp\www\kohana\system\classes\Kohana\Request\Client.php(114): Kohana_Request_Client_Internal->execute_request(Object(Request), Object(Response)) #6 C:\wamp\www\kohana\system\classes\Kohana\Request.php(986): Kohana_Request_Client->execute(Object(Request)) #7 C:\wamp\www\kohana\index.p in C:\wamp\www\kohana\application\classes\Controller\Twitter.php on line 8

and this error

Fatal error: Controller_Twitter::before(): Failed opening required '' (include_path='.;C:\php\pear') in C:\wamp\www\kohana\application\classes\Controller\Twitter.php on line 8

Here is my code

<?php defined('SYSPATH') or die ('No direct script access.');

class Controller_Twitter extends Controller {

    public function before()
    {
        // require_once(APPPATH.'vendor/twitter-api-php/TwitterAPIExchange.php');
        require_once Kohana::find_file('vendor/twitter-api-php', 'TwitterAPIExchange');
    }

    public function action_index()
    {

        $view = new View('twitter/index');
        $this->response->body($view);
    }
}
Это было полезно?

Решение

Put the vendor folder inside the application folder.

The location of the application, modules and system folders can be set in index.php and will be stored in the APPPATH, MODPATH and SYSPATH constants. Kohana::find_files() will look in APPPATH, then in all the loaded modules under MODPATH (ok, modules can be located anywhere, but I am assuming they are all under MODPATH in your case) and finally SYSPATH. Kohana::find_files() won't magically look anywhere else.

Also, why not put the require_once outside the class defenition? Like this.

<?php defined('SYSPATH') or die ('No direct script access.');

// require_once(APPPATH.'vendor/twitter-api-php/TwitterAPIExchange.php');
require_once Kohana::find_file('vendor/twitter-api-php', 'TwitterAPIExchange');

class Controller_Twitter extends Controller {

    public function action_index()
    {
        $view = new View('twitter/index');
        $this->response->body($view);
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top