Solved

Zend Framework - Zend_Loader_Autoloader doesen't work for me

Posted on 2010-09-02
7
781 Views
Last Modified: 2013-11-10
Can't seem to get autoload working.

See screenshots!




*** Bootstrap.php ***
<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{


    protected function _initDoctype()
    {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('XHTML1_STRICT');
    }

    protected function _initAutoloader()
    {
        $loader=Zend_Loader_Autoloader::getInstance();
        $loader->setFallbackAutoloader(true);
    }

}




*** TestController.php ***
<?php

class TestController extends Zend_Controller_Action
{
    public function indexAction()
    {

        $b=get_include_path(); //debug - yes
        $a=new mylittlekalle("littlekalle");
        
    }

}


*** kallbanan.php ***
<?php
class mylittlekalle extends Zend_Form_Element_Select
{
    public function kalle()
    {
        return "kalle";
    }
}


*** index.php ***
<?php
// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));



// Ensure /application is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH ),
    get_include_path(),
)));


// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
            ->run();

Open in new window

dirs.png
error.png
0
Comment
Question by:davidsperling
  • 5
  • 2
7 Comments
 
LVL 6

Expert Comment

by:midhungirish
Comment Utility
Well for autoloding to work, you need to specify the namespace..... You can register a namespace like 'App' and use it as the prefix of the resources..... Also the default autloader settings checks for forms in the "application/forms" directory.. if you want to change it, you need to specify it....  Im not sure the following code works coz i dont have the setup to test it.. Jsut check it out.. may be it will ring a bell....
*** Bootstrap.php ***
<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{


    protected function _initDoctype()
    {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('XHTML1_STRICT');
    }

    protected function _initAutoloader()
    {
        $loader=Zend_Loader_Autoloader::getInstance();
		$loader->registerNamespace('App_');
        $loader->setFallbackAutoloader(true);
    }

}




*** TestController.php ***
<?php

class TestController extends Zend_Controller_Action
{
    public function indexAction()
    {

        $b=get_include_path(); //debug - yes
        $a=new App_Form_mylittlekalle("littlekalle");
        
    }

}


*** forms/mylittlekalle.php ***
<?php
class App_Form_mylittlekalle extends Zend_Form_Element_Select
{
    public function kalle()
    {
        return "kalle";
    }
}


*** index.php ***
<?php
// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));



// Ensure /application is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH ),
    get_include_path(),
)));


// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
            ->run();

Open in new window

0
 
LVL 4

Author Comment

by:davidsperling
Comment Utility
"$loader->setFallbackAutoloader(true)" means that you don't have to register namespaces, doesn't it?

I've also tried with namespaces, but it still doesn't work. Have no idea why.
0
 
LVL 4

Author Comment

by:davidsperling
Comment Utility
... and since /application is in inlude path, classes in /application/kalle should be detected automatically, shouldn't them?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 6

Accepted Solution

by:
midhungirish earned 500 total points
Comment Utility
As a matter of fact $loader->setFallbackAutoloader(true) doesnt mean you dont have tio use name spaces but it means that any name space will be autoloaded.. ie you done need to register it explicitly...... For your code to work, use the name "kalle_mylittlekalle" for the class...... ie 'kalle_' will be the namespace....... so the script looks for kalle/mylittlekalle.php".... Hope you got it...
**kalle/mylittlekalle.php**

<?php
class kalle_mylittlekalle extends Zend_Form_Element_Select
{
    public function kalle()
    {
        return "mylittlekalle new";
    }
}

**TestController.php**
<?php

class TestController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $b=get_include_path(); //debug - yes
        $a=new kalle_mylittlekalle("littlekalle");

    }


}

Open in new window

0
 
LVL 4

Author Comment

by:davidsperling
Comment Utility
I have absolutely no luck at all!

Let's put it this way...

Can you modify the ZF Quick Start project so that  Zend_Loader_Autoloader is used on a custom folder named myclasses (residing in application path) with namespace Mynamespace_?

http://framework.zend.com/demos/ZendFrameworkQuickstart.zip
http://framework.zend.com/demos/ZendFrameworkQuickstart.tar.gz

I don't want to mess around with a lot of "require_once"... That's not why I use ZF!
0
 
LVL 4

Author Comment

by:davidsperling
Comment Utility
Sorry, you were right all along ;)

ee.png
0
 
LVL 4

Author Comment

by:davidsperling
Comment Utility
..
ee2.png
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Mail Not Sent 6 41
echo button 13 45
Trouble parsing soap xml result 3 26
PHP loop not working 4 29
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now