Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Zend Framework - Zend_Loader_Autoloader doesen't work for me

Posted on 2010-09-02
7
Medium Priority
?
796 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 6

Expert Comment

by:midhungirish
ID: 33585231
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
ID: 33585301
"$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
ID: 33585328
... and since /application is in inlude path, classes in /application/kalle should be detected automatically, shouldn't them?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 6

Accepted Solution

by:
midhungirish earned 2000 total points
ID: 33585676
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
ID: 33588004
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
ID: 33588162
Sorry, you were right all along ;)

ee.png
0
 
LVL 4

Author Comment

by:davidsperling
ID: 33588225
..
ee2.png
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

660 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