Namespace declaration statement has to be the very first statement in the script

家住魔仙堡 提交于 2021-02-07 08:02:51

问题


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.

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

AccountRepository

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

Folder Tree

Error Message

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?

Please help.


回答1:


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.

...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



来源:https://stackoverflow.com/questions/31030369/namespace-declaration-statement-has-to-be-the-very-first-statement-in-the-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!