Where to place traits in Laravel 5?

PhpLaravel 5Traits

Php Problem Overview


I am using Laravel 5 and I am confused about where to place traits files in the Laravel 5 directory structure. Should they exist in public, resources or any other directory?

Php Solutions


Solution 1 - Php

In terms of placement you should treat traits like classes. That means put them inside the app directory. Where you place your traits in there depends on your preference and on the actual purpose of the trait.

Important is that you adjust the namespace of the trait to the directory structure. For example if your trait has something to do with your controllers and you decide to put it inside the app/Http/Controllers folder, then make sure it has the correct namespace, which would be:

namespace App\Http\Controllers;

Solution 2 - Php

I prefer creating a directory in app called Traits. This will enhance the readability and maintainability of code base .

Example:

namespace App\Traits;
    
use Exception;
use Illuminate\Http\Request;
    
    trait myTrait {
    //your code here
    }

Solution 3 - Php

You can place trait any where you want. But in terms of placement you should adjust the namespace of the trait to the directory structure.

For example : You want to create a Traits directory Traits >> SayHello.php.

app >> Http >> Controllers >> Traits >> SayHello.php
  <?php
  namespace App\Http\Controllers\Traits; 

  trait SayHello{
        public function hello()
        {
              return "Hello";
        }
  }
app >> Http >> Controllers >> HomeController.php
  <?php
  namespace App\Http\Controllers; 

  use App\Http\Requests;
  use Illuminate\Http\Request;
  use App\Http\Controllers\Traits\SayHello;

  class HomeController extends Controller {

        use SayHello;

        public function index()
        {
              // Called SayHello trait hello method
              return $this->hello();
        }
  }

Solution 4 - Php

Storing Traits

For convenience, create a directory under app called Traits, and placed my custom PHP files there. Each file uses the name of the Trait for logical correlation. By using a namespace of App/Traits in each file the Framework was able to locate my files when I use them in another file.

Sample Usage Code:

<?php
namespace AppJobs;

use App/Jobs/Job;

use App/Traits/ExampleCode;

/**
 *  brief Example code using a Trait in a Laravel "Job"
 */
class AdminLoginJob extends Job implements SelfHandling
{
    use ExampleCode;
    
    /**
     * Call trait to print something. Note the use of "$this".
     * 
     * @return void
     */
    public function __construct()
    {
        $this->printThis();
    }
   /**
     * Do more stuff here.
     * 
     * @return void
     */
     public function handle()
    {
        // never gets called in this example.
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMuhammad Saeed AnwarView Question on Stackoverflow
Solution 1 - PhplukasgeiterView Answer on Stackoverflow
Solution 2 - PhpUser123456View Answer on Stackoverflow
Solution 3 - PhpRashedul Islam SagorView Answer on Stackoverflow
Solution 4 - PhpMr.SenhajiView Answer on Stackoverflow