konstantinos Dms
konstantinos Dms

Reputation: 39

Namespace Laravel Installation

I just installed Laravel under xampp in a project with name blog1. The documentation says that default namespace is App. I create the following controller :

 namespace App\Http\Controllers;
 use App\User;
 use App\Repositories\UserRepository;
 use App\Http\Controllers\Controller;

 class UserController extends Controller
 {
  /**
  * The user repository implementation.
   *
   * @var UserRepository
  */
  protected $users;

/**
 * Create a new controller instance.
 *
 * @param  UserRepository  $users
 * @return void
 */
public function __construct(UserRepository $users)
{
    $this->users = $users;
}

/**
 * Show the profile for the given user.
 *
 * @param  int  $id
 * @return Response
 */
public function show($id)
{
    $user = $this->users->find($id);

    return view('user.profile', ['user' => $user]);
}
}

with the name testController.php under public directory.

when I http://localhost/blog1/testController.php I get the following Class 'App\Http\Controllers\Controller' not found in C:\xampp\htdocs\blog1\public\testController.php on line 10. Any suggestion? I tried to set the namespace to blog1 but now nothing happened with blog1/Http?Controller.

I reinstall Laravel through composer.

Upvotes: 0

Views: 107

Answers (2)

Saumya Rastogi
Saumya Rastogi

Reputation: 13703

The error from laravel is valid. You're placing your file TestController.php under public directory, and giving the class a namespace of - App\Http\Controllers.

You should place your TestController.php inside the directory:

app > Http > Controllers

By this, your code will not throw an error again!

See more about Laravel Controllers & Namespacing & Laravel Directory Structure

Hope this helps!

Upvotes: 0

Grzegorz Gajda
Grzegorz Gajda

Reputation: 2474

Read about PSR-4, PSR-0 (deprecated) and autoloading in PHP. Error is thrown because class name (UserController) must be exact to the filename testController.php so you should name your file UserController.php or your class testController.

Also read about Laravel's routing and composer's autoloading.

Upvotes: 2

Related Questions