Putting names is pretty simple once you get it.
Take the following example:
application / models / file.php
namespace App\Models; class File { public function someMethodThatGetsFiles() { } }
application / controllers / FileController.php
namespace App\Controllers; use App\Models\File; class FileController { public function someMethod() { $file = new File(); } }
Declare a namespace:
namespace App\Controllers;
Remember, once you have placed a class in a namespace to access any of the PHP built-in classes, you need to call them from the root namespace. for example: $stdClass = new stdClass(); will become $stdClass = new \stdClass(); (see \ )
Import other namespaces:
use App\Models\File;
This allows you to use the File class without a namespace prefix.
Alternatively, you can simply call:
$file = new App\Models\File();
But itβs best to put it at the beginning in the use statement, since then you can see all the dependencies between the files without looking at the code.
After that, you need to run composer dump-autoload to update the Composer autoload function to take into account your newly added classes.
Remember that if you want to access the FileController via a URL, you need to define a route and specify the full namespace as follows:
Route::get('file', 'App\\Controllers\\FileController@someMethod');
Which will direct all GET / file requests to someMethod() controller
Take a look at the PHP documentation on Namespaces , and Nettut is always a good resource with this article.