logeeks
logeeks

Reputation: 4979

Laravel 5.X - 'ErrorException' with message 'Use of undefined constant SUCCESS

I am developing a laravel 5.X app and I have defined the variable SUCCESS under the directory 'Helpers' in a file Responses.php as below

if (!defined("SUCCESS")) define("SUCCESS", 111);

I am trying to pass the SUCCESS in the following snippet

    class UsersController extends Controller {

        /**
         * Display a listing of the resource.
         *
         * @return Response
         */
        public function index() {


               $this->manageresponse(SUCCESS, X_WITH_WRONGCARD);

            //
        }
}

The code is refusing to execute by saying

exception 'ErrorException' with message 'Use of undefined constant SUCCESS - assumed 'SUCCESS'' in C:\Users\J...

Please let me know what should I do to access SUCCESS in my controller methods.

Thanks

Upvotes: 0

Views: 913

Answers (2)

Loren
Loren

Reputation: 12831

As far as I know Laravel has no Helpers directory where all the files are autoloaded.

If this is the only thing you are doing, you could define it in app/Http/Kernal.php or app/Providers/RouteServiceProvider.php

To do this "properly," you would need to create your own service provider: https://laravel.com/docs/5.3/providers

Upvotes: 1

treeface
treeface

Reputation: 13351

If you're defining the constant in a particular namespace, you need to access it through that namespace. Even if the constant isn't defined in a namespace, if you're accessing it from within a namespace, you need to use the global \ or put a use statement at the top of your Controller file.

So if it's global, you could do this in your controller:

use Success;

Alternatively from anywhere within the file:

$this->manageresponse(\SUCCESS, X_WITH_WRONGCARD);

or if it's not global, you can do this...

use My\Namespace\Success;

Upvotes: 1

Related Questions