Search code examples
phpautoloaddirectory-structurefat-free-framework

parent class is being searched in wrong location


I am trying to custom create an MVC folder structure on top of the fat-free framework.I have a controllers folder with two files, base.php and index.php. Now I am trying to load the index file like:

$f3->route('GET /','Index->index');

But I get a fatal error:

Fatal error: Class 'controllers\Base' not found

code for base.php:

<?php
namespace controllers;
class Base extends \Prefab {
    protected $f3;

    public function __construct() {
        $this->f3 = \Base::instance();
    }
}

code for index.php

<?php
namespace controllers;
class Index extends Base {

    public function __construct() {
        parent::__construct();
    }

    public function index($f3, $params) {
        echo "Hello World!";
    }
}

Any idea what I am doing wrong?


Solution

  • The controllers folder should be a subfolder of your AUTOLOAD folder. Something like this:

    - autoload
     |- controllers
       |- base.php
       |- index.php
    - lib
     |- base.php
    - index.php
    

    And your root index.php would look like this:

    $f3=require('lib/base.php');
    $f3->set('AUTOLOAD','autoload/');
    $f3->route('GET /','controllers\Index->index');
    

    PS: what happened in your example is that the controllers folder being declared as the autoload folder, base.php and index.php were seen as part of the root namespace.