Yii Framework Forum: Console Autoload Not Working Properly, Compared To Web Application - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Console Autoload Not Working Properly, Compared To Web Application Rate Topic: ***** 1 Votes

#1 User is offline   alpha0436 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 12-September 12

Posted 16 December 2012 - 10:03 PM

Background:
I got a web application, and now I'm extending it with console access for cron jobs in the future.
I'm using 2 separate config files, "main.php" for the Yii Web Application, "console.php" for the Yii Console Application.


Problem:
I'm using modules extensively.
I have build my own "Default Module".
All the other modules extend from this Default Module.

In my web application I make this work by editing the "main.php" config file like this:
// Web Application config

'modules'=>array(
		'default',
		'clients',
		'household'
	),

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


This works, great!


But I want to do the same for the Console part, so to the console config, I add this same snippet of code but it doesn't work....:
When executing my code I will get the following error:

Quote

PHP Error[2]: include(DefaultController.php): failed to open stream: No such fil
e or directory in file K:\web\gpmanager4\framework\YiiBase.php at line 423


I have found a way to fix it though, by adding the following to my "console.php":

	
// Console Application config

'modules'=>array(
		'default',
		'clients',
		'household'
	)

'import'=>array(                 
		'application.components.*',                 
		'application.models.*',    
		'application.modules.default.controllers.*',  // Line I have to add
		'application.modules.default.models.*',     // Line I have to add
		'application.modules.default.views.*',     // Line I have to add
		'application.helpers.*',   
	),  


So this solution makes it work,
but why do I have to do this for the Console and not for the Web ?

My question:

Why do I have to manually add the controllers, models, and views directories of my Default module like this in my "console.php" config file in the IMPORT array,
while I do not have to do this in my Web application "main.php" config file?

I "solved" it this way, but it's dirtier and I don't trust it, because in the future if I'm going to extend from other modules, I might run into the same problems.

Could someone help me with the correct solution, or point out the problem ?
0

#2 User is offline   alpha0436 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 12-September 12

Posted 17 December 2012 - 06:52 PM

Maybe my initial post was a bit unclear.
I've done an Edit and added the Error message to it.
0

#3 User is offline   alpha0436 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 12-September 12

Posted 20 December 2012 - 05:05 AM

I am really stuck on this one, and I can't find any answer.

Excuse my bump :)
0

#4 User is offline   fpolli 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 60
  • Joined: 26-April 12

Posted 21 December 2012 - 06:59 AM

View Postalpha0436, on 20 December 2012 - 05:05 AM, said:

I am really stuck on this one, and I can't find any answer.

Excuse my bump :)


I wish I could help you, but I am having a very similar problem, and I cannot find a solution anywhere. Maybe someone will be able to help one or both of us.

In my situation, I have a set of helper functions in a file named utilities.php located in the components folder. In my web application, I can call these functions normally and everything works fine. But in the console application I am developing now, I get:

Fatal error: Call to undefined function getpreferredoptions() in /path/to/public_html/protected/commands/MyCommand.php on line 9

I copied the import section of my main.php config file into console.php:

<?php

// This is the configuration for yiic console application.
// Any writable CConsoleApplication properties can be configured here.
return array(
	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
	'name'=>'Polli Team Console Components',

	// autoloading model and component classes
	'import'=>array(
		'application.models.*',
		'application.components.*',
		'application.controllers.*',
		'application.commands.*',
		'ext.yii-mail.YiiMailMessage',
		'application.modules.moneyshot.models.*',
	),
);


Like I said, the web app correctly imports utilities.php, but the console application will not. I even added 'application.components.utilities' to console.php, and it still does not work.

I'm dead in the water on this application until I get an answer, and I cannot find anything anywhere.
0

#5 User is offline   mclemay 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 13-November 12

Posted 05 January 2013 - 11:53 AM

A little late, but I ran into this problem, too. In my module's class I do this (the module is called dominion):

class DominionModule extends CWebModule {
    public function __construct($id,$parent,$config = null) {
        parent::__construct($id,$parent,$config);
        $this->setImport(array('dominion.models.*'
                              ,'dominion.controllers.*'
                              ,'dominion.components.*'));
    }
}; // class DominionModule extends CWebModule


I'm thinking in a complex application with many modules this could get out of hand quickly and become a performance issue. In my case this module implements one part of my site (the url will always be /dominion/...). I wonder if I can prevent the module from being loaded unless the url includes /dominion/. That's a project for another day.

Edit: I was forgetting to call the parent constructor.

This post has been edited by mclemay: 06 January 2013 - 01:31 PM

0

#6 User is offline   mclemay 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 13-November 12

Posted 06 January 2013 - 12:44 PM

View Postmclemay, on 05 January 2013 - 11:53 AM, said:

I wonder if I can prevent the module from being loaded unless the url includes /dominion/. That's a project for another day.


Replying to myself, hurr, of course the autoloader will only load my module on demand. I have a chicken-and-an-egg problem, though, where I want my dominion module to be loaded when the url starts with /dominion/ and I want my module to control its own url structure (via CUrlManager::addRules()). The problem is I have to invoke that method in CWebApplication's onBeginRequest; at that point, CHttpRequest hasn't parsed the request at all. If I enumerate the application's configured modules and call getModule() on them, it invokes the autoloadeer on all the module. Including gii, which overrides my custom CWebUser class, along with some other nastiness.

In my DominionModule class I have an addRules() method which adds rules to CUrlManager. But I only want that if the url starts with /dominion/. I guess it's sensible for my onBeginRequest handler to get the first part of the url to look for 'dominion'. I could be a little smarter about it and take the first part of the url and compare it to the application's modules, and if it matches, call getModule() on that, check method_exists to see if it has an addRules method, and then invoke it. Here's what I wound up with for my onBeginRequest:

    function() {
        $uri = $_SERVER['REQUEST_URI'];
        // get rid of GET params & anchor names
        $uri = preg_replace('/[?#].*$/','',$uri);
        // replace duplicate slashes.
        $uri = preg_replace('{/{2,}}','/',$uri);
        // remove leading/trailing slashes
        $uri = trim($uri,'/');
        // get the first part of the uri
        $uri = explode('/',$uri,2);
        if(empty($uri)) {
            return;
        }
        foreach(Yii::app()->modules as $id => $config) {
            if($id == $uri[0]) {
                // i.e., this module name matches the first part of the uri
                $module = Yii::app()->getModule($id);
                if(method_exists($module,'addRules')) {
                    // i.e., this module has a method named addRules
                    $module->addRules();
                }
                return;
            }
        }
    }


I'm not terribly pleased with this solution, because it feels ambiguous with the url manager, and in the onBeginRequest since the url hasn't been parsed, I have no way of knowing if the first component of the url is path info. I would love to hear other solutions to this problem.
0

#7 User is offline   alpha0436 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 12-September 12

Posted 16 January 2013 - 05:15 PM

You are not late.

This topic is still unsolved and I haven't found a solution thus far :(

I really hope someone can help us with this.
0

#8 User is offline   rAWTAZ 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 102
  • Joined: 08-January 10

Posted 19 January 2013 - 09:17 AM

OP: I thought you were going to create a minimal test application that we can use to look into this problem?
0

#9 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 19 January 2013 - 09:40 AM

Use 'preload' ?
"Less noise - more signal"
0

#10 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 530
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 15 April 2013 - 06:55 AM

Maybe this helps: https://github.com/y...omment-16380420
0

#11 User is offline   nguyendh 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 21-March 11

Posted 27 April 2013 - 06:38 PM

I accidentally found out why helpers file is not included for console application. I believe whatever CeBe said is correct. The reason helpers file is included for my web application because I have

require_once( dirname(__FILE__) . '/../components/helpers.php');


in config.main.php

I bet if I put the require_once in console.php. It will work.
I am new to Yii and this is my blog http://blog.michaelduy.com (deprecated hatadu.wordpress.com) about Yii, Git and PHP.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users