1

I can't wrap my head around this, as I have this on another project and works just fine. It is currently giving me this error

Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN) Trait 'App\Traits\ResponseTrait' not found

The UserRepository works just fine, the trait does not. I've tried renaming the trait, moving it to the repository folder, chmod it to 777, and I have no idea what could be wrong.

Meet my HomeController, the laravel's default:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Repositories\UserRepository as User;
use App\Traits\ResponseTrait;

class HomeController extends Controller
{
  use ResponseTrait;

  private $user;

  /**
  * Create a new controller instance.
  *
  * @return void
  */
  public function __construct(User $user)
  {
    $this->user = $user;
    $this->middleware('auth');
  }

  /**
  * Show the application dashboard.
  *
  * @return \Illuminate\Http\Response
  */
  public function index()
  {
    //dd($this->user->all()->toArray());
    //You can ignore the self:: part, as it doesn't reach this far.
    self::setData($this->user->all()->toArray());
    return "test";
    return view('home');
  }
}

The trait:

<?

namespace App\Traits;

trait ResponseTrait {
  public function setData($array) {

  }
}

The trait's path is /app/Traits/ResponseTrait.php

At my composer.json, I've got PSR-4 autoload with the App directive.

"psr-4": {
   "App\\": "app/"
}

Edit1: I have tried composer dump-autoload, issue remains

10
  • 2
    try composer dump-autoload Commented Jan 4, 2018 at 12:50
  • 1
    if you have a remote server and you work locally try to don't forget to upload your code. Sometime happens. Commented Jan 4, 2018 at 12:52
  • 2
    Do you have the php code delimiters / tags in ResponseTrait.php? Looking at your code, I don't think there are. Commented Jan 4, 2018 at 12:54
  • 1
    is your filename and trait name same?? Commented Jan 4, 2018 at 12:54
  • 2
    Don't use <?, always use <?php. Commented Jan 4, 2018 at 12:56

1 Answer 1

10

Don't use <?, always use <?php.

Short open tags need to be enabled in php.ini and are not widely supported because they conflict with other languages.

Sign up to request clarification or add additional context in comments.

1 Comment

Can't believe I've completely missed the php part, thanks for pointing it out. Problem solved

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.