alex.lara
3 years ago

How to use consts in Laravel blade templates?

Posted 3 years ago by alex.lara

Assume I would like to port a plain PHP app to Laravel. There is Settings class

class Settings
{
    const SID = "500000";
    const SECRET = "abscdefg";
}

// Clean access to constant
Settings::SID

Laravel suggests to use configs for such constants (am I right?)

[
    'SID' => '500000',
    'SECRET' = 'abscdefg'
]

The problem is that now instead of having identifiers (Settings::SID) I get magic strings everywhere (Config::get('SID'), or similar notation). What's the point? What benefits such strings create?

Now, In blade templates I still could use

// Auto completion works, while for facades it doesn't
{{ \App\Http\Settings\Settings::SID }}

instead of dealing with Config::get('SID'), however such long notation would quickly kill readability. I guess in blade template I could use

<?php
use \App\Http\Settings\Settings as Settings
?>
// Good enough but no auto-complete in PHPStorm
{{ Settings::SID }}

however using "use" in blade templates doesn't look right to me.

So what's the right way of handling [class] constants in Laravel blade templates?

Please sign in or create an account to participate in this conversation.