Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

laravel

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.

like image 456
tsuta Avatar asked Mar 16 '23 15:03

tsuta


1 Answers

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

like image 180
tsuta Avatar answered Mar 18 '23 03:03

tsuta