The pathnames throughout a project must be obvious by either mapping directly to a class's namespace, or relate to a requested URL.
Assuming a project's root directory is at /app
, and the namespace root for MyApp
is set to ./src/class
:
/app/src/class/User/Settings.php
:
<?php
namepsace MyApp\User;
class Settings {
// ...
}
If a PHP class acts as a controller for page logic its pathname must correspond to the requested URL it serves. Hyphens in URLs are kept in filenames, but are dropped in class names, upper-casing the words instead.
A consistent set of rules must be used. Assuming a project's root directory is at /app
, the base page directory is set to ./src/page
, and the class suffix for pages is Page
:
URL requested http://example.com/user/advanced-settings
/app/src/page/user/advanced-settings.php
:
<?php
namespace MyApp\Page\User;
class AdvancedSettingsPage {
// ...
}
URL requested: http://example.com/post-list
/app/src/page/post-list.php
:
<?php
namespace MyApp\Page;
class PostListPage {
// ...
}
When using namespace-mapped classes, a PHP file's pathname must correspond to its fully qualified class name with respect to PSR-4 namespace root(s) defined in the project.
composer.json
{
"autoload": {
"psr-4": {
"MyApp\\": "src/class"
}
}
}
/app/src/class/Model/User.php
:
<?php
namespace MyApp\Model;
class User {
// ...
}