gpt4 book ai didi

php - 命名空间声明语句必须是脚本中的第一个语句

转载 作者:行者123 更新时间:2023-12-01 11:32:37 27 4
gpt4 key购买 nike

我刚开始使用 Laravel 开发 Web 应用程序,我在使用依赖注入(inject)时遇到了问题。没有 DI 也能正常工作,但我想重构代码以使代码不紧密耦合。

我已经在 google 中搜索,表明命名空间之前可能有一个空格并在此处搜索相关问题,但它们都没有解决我的问题。

账户 Controller

<?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;
}
}

文件夹树

Folder Tree

错误信息

FatalErrorException in AccountRepository.php line 3: Namespace declaration statement has to be the very first statement in the script

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


我是否错过了任何重要的配置,例如服务定位器设置或只是看不见的代码错误?

请帮忙。

最佳答案

它与依赖注入(inject)无关,根据 kuzawinski 对手册的评论,我用记事本重新创建了文件,它解决了问题。

...and you still get "Namespace declaration statement has to be the very first statement in the script" Fatal error, then you probably use UTF-8 encoding (which is good) with Byte Order Mark, aka BOM (which is bad). Try to convert your files to "UTF-8 without BOM", and it should be ok. Comment

关于php - 命名空间声明语句必须是脚本中的第一个语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31030369/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com