我刚开始使用Laravel开发Web应用程序,但是使用依赖注入时遇到了问题。没有DI,它可以正常工作,但我想重构代码,以使代码不会紧密耦合。
我已经在google中搜索过,建议在名称空间之前可能有一个空格,并在此处搜索相关问题,但是没有一个可以解决我的问题。
AccountController
<?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);
}
}
帐户存储库
<?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;
}
}
文件夹树
错误信息
AccountRepository.php第3行中的FatalErrorException:名称空间声明语句必须是脚本中的第一条语句
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
我是否错过了任何重要的配置,例如服务定位器设置或只是看不见的代码错误?
请帮忙。
它与依赖项注入无关,基于手册上的kuzawinski注释,我使用记事本重新创建了文件,从而解决了该问题。
...并且您仍然收到“命名空间声明语句必须是脚本中的第一个语句”的致命错误,然后您可能使用带字节顺序标记的UTF-8编码(这很好),又称为BOM(不好) 。尝试将文件转换为“没有BOM的UTF-8”,应该没问题。评论
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句