0

My project worked fine until I decided to implement namespaces. Since then I've been having some problems. I familiarized myself with this post that had similar issues, but no luck. I also read the documentation about namespaces but it didn't help either.

My file structure looks like:

-fatfree
  -index.php
  -app
    -config.ini
    -routes.ini
    -controllers
      -Controller.php
      -DeviceController.php
    -models
      -*.php
    -views
      -*.html
  -lib
    base.php
    ...

My routes.ini file looks like:

[routes]

GET @devices: /devices = \Controllers\DeviceController->devices

My DeviceController class looks like:

<?php

namespace Controllers;

class DeviceController extends \Controller
{
    public function devices($f3)
    {
        ...
    }

    ...
}

My index.php file looks like:

<?php

$f3 = require("lib/base.php");
$f3->config("app/config.ini");
$f3->config("app/routes.ini");

new Session();

$f3->run();

When I navigate to the devices page I get the following error:

Not Found

HTTP 404 (GET /devices)

[/fatfree/lib/base.php:1462] Base->error(404) [/fatfree/index.php:13] Base->run()

Community
  • 1
  • 1
roundtheworld
  • 2,651
  • 4
  • 32
  • 51

1 Answers1

1

Have you tried renaming your controller files to controller.php and devicecontroller.php?

dev
  • 31
  • 4
  • 1
    Are your models also namespaced? If so you may need to use `$this->devicesModel = new \ModelsNS\DevicesModel($db);` Can you set the DEBUG variable higher (2 or 3) so we can see a bigger error trace? – dev Oct 11 '16 at 15:47
  • 1
    I think it should just be `namespace Models;` without the leading backslash. Not sure if that'll make a difference. I use a setup like this and it works. On my controllers I need to call `$model = new \Models\Mymodel;` – dev Oct 11 '16 at 15:57
  • 1
    Ah, and also I think you should use `extends Controller` instead of `extends \Controller` – dev Oct 11 '16 at 16:00
  • 1
    Yes, once you're using namespaces you'll have to use the backslash on most calls, including Base, DB's, Session and pretty much everything. Make sure all your `extends` aren't using a backslash – dev Oct 11 '16 at 16:15
  • 1
    Well, in that case, since you're extending a core F3 class you should use the backslash in the beginning like `extends \DB\SQL\Mapper`. Only if extending another file you've placed inside your Controllers or Models folder then you shouldn't use the backslash. – dev Oct 11 '16 at 16:26
  • 1
    That error message got cropped it seems? StackOverflow comments aren't very good for this. We'll need to see what's on those lines the error trace mentions. – dev Oct 11 '16 at 16:32
  • 1
    what's on controller.php line 22? – dev Oct 11 '16 at 16:37
  • 1
    Can you post controller.php on pastebin? – dev Oct 11 '16 at 16:42
  • 1
    That is indeed a strange error. Searching the web for that specific error brings up a lot of other people using other frameworks with that same error and it seems to have something to do with the database setup. You mentioned that your app was working before all this? So, mysql is running correctly? – dev Oct 11 '16 at 16:50
  • 1
    Are all database settings variables setup correctly? user/pass/name? On the error trace it seems they're empty although it's possible that it doesn't show on the error trace for security concerns. But anyway, please confirm or hardcode all parameters to test it out. – dev Oct 11 '16 at 16:57
  • 1
    Just saw that on that github, on controller.php you have "DB\SQL" and not "\DB\SQL" when setting $db although a few posts above you stated that you've added the backslash there. Could that be it? – dev Oct 11 '16 at 17:20
  • 1
    What about this on your DevicesModel constructor: `public function __construct(DB\SQL $db)` ? It should be `public function __construct(\DB\SQL $db)` or just `public function __construct($db)` – dev Oct 11 '16 at 17:49
  • 2
    That depends on the autoloader being used as well as the filesystem being used. For example, windows treats all cases the same while most (or all?) Linux distros treat them differently. – dev Oct 11 '16 at 18:17
  • 1
    No problem, glad it worked. As for naming files, not really sure, but you can take a look at base.php (search for spl_autoload_register and see the autoload method) to see how the framework's autoloader is implemented. Maybe you can create an autoloader yourself that follows the casing you want. – dev Oct 11 '16 at 20:37