Jump to content

Recommended Posts

Wasn't sure which section to post this question in so putting it here...

 

I am making my own app and trying to implement namespacing with composer but it isn't working. 

 

The directory structure of my application is as follows:

 

/public_html

----/public

--------index.php

----/src

--------LoginController.php

----/vendor

--------/phpunit

--------/composer

--------autoload.php

----composer.json

 

In composer.json I have:

    "autoload": {
        "psr-4": {
            "Bills\\" : "src/"
        }
    }

In my index.php I have:

	require_once __DIR__ . '/../vendor/autoload.php';

	use Bills\LoginController;

     	$actions = explode("/", $_SERVER['REQUEST_URI']);
	
	if($actions[1] != '') {
		$name = ucfirst($actions[1]) . "Controller";
		$object = new $name($secret);
	}   

In LoginController.php I have 'namespace Bills;' at the top.

 

But when I run it I get the error:

 

 Uncaught Error: Class 'LoginController' not found in /var/www/***/public_html/public/index.php

 

Not sure what's going wrong... 

Edited by DefiantPanda
Link to comment
https://forums.phpfreaks.com/topic/307058-namespacing-not-working-properly/
Share on other sites

Yeah... I got it working by doing this - 

		$name = ucfirst($actions[1]) . "Controller";
		$controller = "Bills\\" . $name;
		$object = new $controller($secret);

so it's new Bills\LoginController(); Not sure if that's how you're meant to do it, but it works so that's a relief

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.