Namespaces Convertion

Published 2 months ago by tealiedie

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

// folder structure
app
  -- 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
<?php
namespace App;

in my controllers i use the class as..

<?php

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.

ejdelmonico

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

tealiedie

@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?

thanks

ejdelmonico

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.

\<NamespaceName>(\<SubNamespaceNames>)*\<ClassName>

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