Reputation: 551
I Just begin with Laravel 5 and When I try to run php artisan route:list
to get the liste route, I get this error message :
PHP Fatal error: Namespace declaration statement has to be the very first statement in the script in /var/www/laravel5/app/Http/Controllers/UserController.php on line 1
[Symfony\Component\Debug\Exception\FatalErrorException]
Namespace declaration statement has to be the very first statement in the script
UserController.php:
<?php
namespace App\Http\Controllers;
use App\Http\Requests\UserCreateRequest;
use App\Http\Requests\UserUpdateRequest;
use App\Repositories\UserRepository;
use Illuminate\Http\Request;
class UserController extends Controller
{
protected $userRepository;
protected $nbrPerPage = 4;
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
public function index()
{
$users = $this->userRepository->getPaginate($this->nbrPerPage);
$links = $users->setPath('')->render());
return view('index', compact('users', 'links'));
}
public function create()
{
return view('create');
}
public function store(UserCreateRequest $request)
{
$user = $this->userRepository->store($request->all());
return redirect('user')->withOk("L'utilisateur " . $user->name . " a été créé.");
}
public function show($id)
{
$user = $this->userRepository->getById($id);
return view('show', compact('user'));
}
public function edit($id)
{
$user = $this->userRepository->getById($id);
return view('edit', compact('user'));
}
public function update(UserUpdateRequest $request, $id)
{
$this->userRepository->update($id, $request->all());
return redirect('user')->withOk("L'utilisateur " . $request->input('name') . " a été modifié.");
}
public function destroy($id)
{
$this->userRepository->destroy($id);
return redirect()->back();
}
}
So when I used userController.php generated by php artisan make:controller UserController
I didn't get error. This is when I override the default controller by adding __construct()
Upvotes: 3
Views: 2330
Reputation: 608
namespace App\Http\Controllers; it should be on first line and in your user controller after namespace add the below line
use App\Http\Controllers\Controller;
and Try to execute command now it will work!!
Upvotes: 1
Reputation: 34924
I also faced this issue, In my case I just removed all blank spaces before <?php
and my problem was resolved.
Upvotes: 1