PHP class name with variable name and namespace

I use PHP5 to create my own MVC framework. I have a situation where all my controllers are in the Controller namespace, for example:

namespace Controllers
{
    class Home extends \Framework\Core\Controller 
    {
        ...
     }
}

In the application logic, I need to select the controller for the call from the URL passed from the call, as follows:

namespace Framework\Core;

class Application 
{
     protected $controllerName = 'home';
     protected $methodName = 'index';
     protected $controller;

     public function __construct()
     {
          // Parse the received  URL
          $url = parseUrl();

          // Choose the controller, if file exists
          if (file_exists('../app/controllers/' . $url[0] . '.php'))
          {
              $this->controllerName = $url[0];
              unset($url[0]);
          }

          require_once '../app/controllers/' . $this->controllerName . '.php';

          // Call the controller
          $objToCall = '\\Controllers\\' . $this->controllerName;
          $this->controller = new $objToCall(); <<<======= PROBLEM HERE

          ... Proceed choosing the method and calling the objects method....

          }
    }
}

My problem:

My controller file is called home.php, and my class is called Home(first letter in uppercase). Therefore, the above code cannot find the class /Controllers/home, because the class/Controllers/home

On the other hand, if I remove the namespace from the controller and call:

$this->controller = new $this->controllerName;

It works great, even the name Home....

My questions:

a) Why is the house transferred to Home if I don't have namespaces?

b) ?

, , , CustomerReceipt, PersonalCashWithdrawl ..

.

+4
2

, , .

  • /controllers/
  • ,
  • .

, :

  • home.php
  • RecipeForDisaster.php
  • AnotherController.php

:

Key                   Value
--------------------  ----------------------------
home                  Home.php
recipefordisaster     RecipeForDisaster.php
anothercontroller     AnotherController.php

, ( ) .

, , , , . ? (Linux, OSX,...) home.php home.php . , ? , , , 2 .

, , , PHP . . , :

class myclass {
    public function printme() {
        print 'hello world';
    }
}

$a = new myclass();     // Correct case
$a->printme();

$b = new MyClass();     // "Incorrect" case... but still working
$b->printme();

, . , , .

, , , .

: ( ), , .

Routing

, . , "" . , , , -.

. , , , , , .

, , :

  • , , . , . , 2.
  • , . , . , 3.
  • , , "" ( ).

: , ? ...

+3

. . , , , ucfirst() URL. ( , )

, :

a) , . Windows , linux , . , - , PHP . ( Maxime)

b) , . URL-, . , , , , , , .

In addition, I was on the verge of creating my own structure, and I could give you some advice if you are interested.

+2
source

All Articles