How to share a variable across all views?

前端 未结 2 2019
自闭症患者
自闭症患者 2020-12-06 17:40

I want information about the system locale to be available in every view, so I could highlight whatever language is currently selected by a user. After some googling around,

2条回答
  •  北海茫月
    2020-12-06 17:57

    For people with small project:

    Firstly, The accepted answer is awesome!

    For Laravel 5.2 users:

    Just use the new blade directive @inject within your views like this

    @inject('shared','App\Utilities\SharedWithView')

    then you can use it: {{ $shared->functionName() }}

    And SharedWithView is a simple class like this one:

    namespace App\Utilities;
    
    use App\Repositories\SomeRepositoryLikeArticlesRepository;
    
    class SharedWithView {
    
        public function functionName() {
            $properNameHere = new SomeRepositoryLikeArticlesRepository();
    
            return $properNameHere->forEaxmpleGetMostViewedArticles( 10 );
        }
    
    }
    

提交回复
热议问题