Laravel 5中的致命错误

时间:2016-06-21 08:08:54

标签: php laravel-5

我刚开始使用Laravel 5,当我尝试运行php artisan route:list来获取liste路由时,我收到以下错误消息:

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();
    }

}

因此,当我使用php artisan make:controller UserController生成的userController.php时,我没有收到错误。这是我通过添加__construct()

覆盖默认控制器的时候

2 个答案:

答案 0 :(得分:1)

我也遇到过这个问题,在我的情况下,我刚刚删除<?php之前的所有空格,我的问题已经解决了。

答案 1 :(得分:1)

命名空间App \ Http \ Controllers;它应该在第一线和 在命名空间后添加以下行

use App\Http\Controllers\Controller; 

并尝试执行命令现在它将起作用!!