Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Console Application does not auto-import correctly as Web Application #2344

Closed
duynguyen0511 opened this Issue · 3 comments

3 participants

@duynguyen0511

Here is my console.php which is same as main.php

    // console.php
    return array(
    'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',

    // preloading 'log' component
    'preload'=>array('log','application.components.helpers'),

    // autoloading model and component classes
    'import'=>array(
        'application.models.*',
        'application.components.*',
        'application.modules.user.models.*',
        'application.modules.user.components.*',
        'application.extensions.CAdvancedArBehavior',
        'application.extensions.wideimage.WideImage',
    ),

I have some helpers functions in /protected/components/helpers.php. The web application has no problem access those functions. However, console application yields error

   Fatal error: Call to undefined fuction xml_attributes() 

An disscussion of this problem is also found at (http://www.yiiframework.com/forum/index.php/topic/38599-console-autoload-not-working-properly-compared-to-web-application/)

I tried

       'preload'=>array('log','application.components.helpers'),
      'import'=>array(
        'application.components.*',
                'application.components.helpers',
    ),

but it still does not import helpers

@cebe
Owner

import and preload is ment to be used for class autoloading. This will only work for classes that follow the naming convention used by yii. When you want to include files that contain functions you have to use php require_once or include_once functions to load the file.

@cebe cebe closed this
@duynguyen0511

I understand your argument of Yii convention. However, the fact that import works for Web Application, but does not for Console Application really bothers me.

@resurtm
Collaborator

@duynguyen0511, your code is invalid:

'preload'=>array('log','application.components.helpers'),

First of all, please refer to the CModule::$preload property's PHPDoc. It states:

/**
 * @var array the IDs of the application components that should be preloaded.
 */
public $preload=array();

You must not specify path aliasies in this property. It accepts only unique IDs of the application components. I suggest you to remove error in your code and never use preload that way.

Second, please see source code of the setImport() method—it internally uses Yii::import(). PHPDoc of the Yii::import() method:

/**
 * Imports a class or a directory.
 *
 * Importing a class is like including the corresponding class file.
 * The main difference is that importing a class is much lighter because it only
 * includes the class file when the class is referenced the first time.
 *
 * Importing a directory is equivalent to adding a directory into the PHP include path.
 * If multiple directories are imported, the directories imported later will take
 * precedence in class file searching (i.e., they are added to the front of the PHP include path).
 *
 * ...
 */

It does not include PHP files (with functions, etc.), it imports class or directory.

However, the fact that import works for Web Application, but does not for Console Application really bothers me.

Even if it used to work for web and didn't work for console application it still remains as side effect. Do not get us wrong, core team is not responsible if anyone uses undocumented and unusual peculiarities of the code.

I have some helpers functions in /protected/components/helpers.php. The web application has no problem access those functions. However, console application yields error

I would like to suggest you to include or require helpers file somewhere in your front controller file (index.php). Please refer to this wiki article for more details on how global functions/helpers could be used in a proper way: Use shortcut functions to reduce typing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.