Namespaces Convertion

Published 11 months ago by tealiedie

Hi, i am not sure if this is possible in PHP namespaces. please help me

// folder structure
  -- ModelsFolder // the models folder
    -- ComponentsFolder // some traits
      -- Traits::class

    -- User::class
    -- OtherModel::class

the Classes are only using the app folder as namespace

// User class
namespace App;

in my controllers i use the class as..


namespace App\Http\Controllers;

use App\User; // import class
use App\OtherModel; 

i wonder if this setup is possible, because it's giving me FatalError saying the Class 'App\User' not found.


It looks to me like you have User under App\ModelsFolder\User


@ejdelmonico so there's no way i can use the models without having to include the ModelsFolder in namespace? do i need to change the namespace according to the folder structure?



You have it. Just look around the Laravel directories and you will see how the namespaces relate to all files. For instance, User.php is located in the app directory, thus the namespace is App\User. Another...the controller.php file is located in the app directory...then the Http directory...Controllers directory. The namespace is App\Http\Controllers\. You get the drift. So, if you locate your modals in a different directory in app, then correct the namespace accordingly.


Please sign in or create an account to participate in this conversation.