Often it is necessary to use class constants in views, which implies the full name of the class with namespace, which leads to healthy unreadable conditions. Question: Is it possible to use analog use \vendor\ClassName in a viewer in some way?

  • Add alias or forward ime class to template variable? - Naumov
  • Can you give more details? example? - Oleg Shakhov

1 answer 1

One way to do this is an example from the documentation:

 @inject('metrics', 'App\Services\MetricsService') <div> Monthly Revenue: {{ $metrics->monthlyRevenue() }}. </div> 

You can also enter variables that will be visible in all Templates:

For example, in App \ Providers \ AppServiceProvider

 public function boot(){ //...что то до этого например создаешь экземпляр класса obj \View::share('obj',$obj); } 
  • And if I need to access a class constant and not an object's methods? - Oleg Shakhov
  • @Oleg Shakhov Create a helper class for example or a container service that will return what you need - Orange_shadow
  • @Oleg Shakhov In general, if you have a class of constants alone, then it is easier for you to create a file in the config file, and from there take conf('my_settings_file.my_const_name'); - Orange_shadow
  • I have a class - a model that has statuses in the form of constants and now we need to check whether the status of the model corresponds to a specific constant. - Oleg Shakhov
  • @Oleg Shakhov I didn’t understand, even if you have an instance of a class passed to a constant there and apply, do you need an auxiliary function for the instance, which you will need to check. And in general, give an example of a clear, how you have and what you want to do - Orange_shadow