1

I am working on some legacy code which has Controller class which extends Laravels native Controller. Inside that class I have constant statuses:

class Controller extends \Controller
{
    const STATUS_SUCCESS = 'success';
    const STATUS_ERROR = 'error';
....

Fist thing that bothers me is the fact that there are statuses inside a controller class, and I don't think they should be there by good design, but rather on some other ControllerStatus class.

Another thing about it is that those statuses are repeated in several unrelated classes, so I am wondering if it is a good practice to make a generic class Status which will hold possible statuses?

The possible issue I see here is that every class that needs to return a status now in return instead of being coupled only to say Controller, needs to be coupled with Status as well.

How would I best approach this?

Norgul
  • 4,008
  • 8
  • 40
  • 103

2 Answers2

1

If constants are being used globally then storing them in config files is a simple and effective solution.

You should create a new file as constants.php

In that file, you can write an array of a constant which you need to use throughout your application and you can easily add new constants in that file.

return [
    'status' => [
        'status_success' => 'success',
        'status_error' => 'error',
        'status_abort' => 'abort',
    ]
];

And you can access them as mentioned below

Config::get('constants.status');
// or if you want a specific one
Config::get('constants.status.status_success');

As you have said using class is also fine but you have to import that class everywhere and if you are using lot of constants in your application then you will end up creating the separate class for different constants.

But in my personal openion using this Config::get('constants.status.status_success'); looks good and your code becomes self documenting. A programmer should write a code which is self-documenting. Also you can easily add new constants so you can use Config::get('constants.pay_type.visa');

But in the end, decide what is beneficial in your application and use that.

Suraj
  • 1,765
  • 2
  • 14
  • 25
0

You could perhaps use an interface to ensure that all required controllers have the same constants?

Pros and Cons of Interface constants

The only alternative is making an Abstract class that extends the base Laravel controller which contains your constants, and then you extend that abstract class?

Jono20201
  • 3,093
  • 2
  • 18
  • 32
  • Is there any plus to using interfaces instead of classes in this case? – Norgul Nov 06 '17 at 16:35
  • In all honesty in your case it seems to be some code smell that I'd investigate further, but I don't know your application or the exact task your trying to do. I'd prefer an interface in from the information, seems cleaner than doing extending for a few constants. – Jono20201 Nov 06 '17 at 16:45
  • I'm not extending, I'm just using it in the class header and then calling like `Status::ERROR` since I basically want it to be available as a global variable, as it is not likely to change during the whole lifetime of the project – Norgul Nov 06 '17 at 17:03
  • @Norgul Yes, you can create a separate class and use that everywhere. – Suraj Nov 07 '17 at 04:27