Wednesday, March 1, 2017

Class from another namespace not found in laravel

I have recently learned the concepts of namespaces in PHP and just want to use them in my Laravel project. To do this, I decided to first test what I've learned and created the following codes in my controller and a custom class that I myself created and are as follows :

app\Http\Controllers\HomeController.php :

<?php
namespace App\Http\Controllers;

use App\Http\Classes\UserTables as userTables;
use Illuminate\Http\Request; 

class HomeController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        //return view('home');
        $result = new userTables;
        $result->CheckCreate();
    }
}


app\Http\Controllers\Classes\UserTables.class.php :

<?php
namespace App\Http\Classes\

class UserTables
{
    public function CheckCreate ()
    {
        $user_id = Auth::user()->id;
        $user_name = Auth::user()->name;
        $table_name = $user_name . '_' . $user_id;
        echo $table_name;
    }
}


As I have tested the concept before and it was working properly on the localhost and not in the body of Laravel, I wonder why it now returns the fatal error of :

FatalErrorException in HomeController.php line 27: Class 'App\Http\Classes\UserTables' not found 1. in HomeController.php line 27


Should I have included and imported the class file before using it and if so what will be the benefit of using namespaces?




via Tower

Advertisement