我刚开始用 Laravel 开发 Web 应用程序,我在使用依赖注入时遇到了问题。它在没有 DI 的情况下工作正常,但我想重构代码以使代码不紧密耦合。
我已经在 google 中搜索,这表明名称空间之前可能有一个空格,并在此处搜索相关问题,但没有一个能解决我的问题。
帐户控制器
<?php
namespace TabJut\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;
use View;
use TabJut\Http\Requests;
use TabJut\Http\Controllers\Controller;
use TabJut\Repositories\AccountRepository;
class AccountController extends Controller
{
/* error culprit, If I remove these the page not error */
protected $repository;
public function __construct(AccountRepository $repository)
{
$this->repository = $repository;
}
/* error culprit */
public function getLogin()
{
return View::make('account.login');
}
public function postLogin()
{
// Validates inputs.
$rules = array(
'username' => 'required',
'password' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
// Redirects back to the form if the validator fails.
if ($validator->fails()) {
return Redirect::action('AccountController@getLogin')
->withErrors($validator)
->withInput(Input::except('password'));
}
$username = Input::get('username');
$password = Input::get('password');
$user = $repository.Authenticate($username, $password);
var_dump($user);
}
}
Run Code Online (Sandbox Code Playgroud)
帐户库
<?php
namespace TabJut\Repositories;
use DB;
class AccountRepository
{
public function Authenticate($username, $password)
{
$user = DB::table('users')
->where('is_active', '1')
->where('user_name', $username)
->where('password', $password)
->first();
return $user;
}
}
Run Code Online (Sandbox Code Playgroud)
文件夹树

错误信息
AccountRepository.php 第 3 行中的 FatalErrorException:命名空间声明语句必须是脚本中的第一个语句
Run Code Online (Sandbox Code Playgroud)in AccountRepository.php line 3 at FatalErrorException->__construct() in HandleExceptions.php line 127 at HandleExceptions->fatalExceptionFromError() in HandleExceptions.php line 112 at HandleExceptions->handleShutdown() in HandleExceptions.php line 0 at Composer\Autoload\includeFile() in ClassLoader.php line 301
我是否错过了任何重要的配置,如服务定位器设置或只是看不见的代码错误?
请帮忙。