命名空间声明语句必须是脚本中的第一条语句 [英] Namespace declaration statement has to be the very first statement in the script

查看:40
本文介绍了命名空间声明语句必须是脚本中的第一条语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Laravel开发Web应用程序,但是使用依赖项注入时遇到了问题.在没有DI的情况下,它可以正常工作,但是我想重构代码,以使代码不会紧密耦合.

I just started to develop web application with Laravel, I have a problem to use the dependency injection. It works fine without the DI, but I want to refactor the code so that the code is not tightly coupled.

我已经在google中搜索过,提示​​在名称空间之前可能存在空白,并在此处搜索相关问题,但是没有一个问题可以解决我的问题.

I already search in google that suggests perhaps there is a white space before the namespace and search related questions here, but none of them solve my problem.

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:命名空间声明语句必须是脚本中的第一条语句

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

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

Did I miss any important configuration like service locator setup or just unseen code error?

请帮助.

推荐答案

与依赖项注入无关,基于手册上的kuzawinski注释,我使用记事本重新创建了文件并解决了问题.

It has nothing to do with the dependency injection, based on kuzawinski comment on the manual, I recreated the file with notepad and it solves the problem.

...,您仍然会收到命名空间声明语句必须是脚本中的第一个语句致命错误,那么您可能使用UTF-8编码(很好),带有字节顺序标记,又称为BOM(这是坏的).尝试将文件转换为没有BOM的UTF-8",它应该没事.评论

这篇关于命名空间声明语句必须是脚本中的第一条语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆