PHP Snippet 1:
class BaseController extends Controller
{
public function __construct()
{
//its just a dummy data object.
$user = User::all();
// Sharing is caring
View::share('user', $user);
}
}
PHP Snippet 2:
App::before(function($request)
{
// Set up global user object for views
View::share('user', User::all());
});
PHP Snippet 3:
Route::filter('user-filter', function() {
View::share('user', User::all());
});
PHP Snippet 4:
Route::group(['middleware' => 'SomeMiddleware'], function(){
// routes
});
class SomeMiddleware {
public function handle($request)
{
\View::share('user', auth()->user());
}
}
PHP Snippet 5:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider {
public function boot() {
view()->composer("ViewName","App\Http\ViewComposers\TestViewComposer");
}
}
PHP Snippet 6:
namespace App\Http\ViewComposers;
use Illuminate\Contracts\View\View;
class TestViewComposer {
public function compose(View $view) {
$view->with('ViewComposerTestVariable', "Calling with View Composer Provider");
}
}
PHP Snippet 7:
Here you are... {{$ViewComposerTestVariable}}
PHP Snippet 8:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider {
public function boot() {
view()->composer('*',"App\Http\ViewComposers\TestViewComposer");
}
}
PHP Snippet 9:
public function boot() {
view()->share('data', [1, 2, 3]);
}
PHP Snippet 10:
<?php
namespace App\Providers;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
class ViewServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
* @return void
*/
public function boot()
{
view()->composer('*', function ($view)
{
$user = request()->user();
$view->with('user', $user);
});
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
PHP Snippet 11:
<?php
// from AppServiceProvider::boot()
$viewFactory = $this->app->make(Factory::class);
$viewFacrory->compose('*', GlobalComposer::class);
PHP Snippet 12:
@for($i = 0; $i<1000; $i++)
@include('some_partial_view_to_display_i', ['toDisplay' => $i])
@endfor
PHP Snippet 13:
Route::group(['middleware' => 'WebMiddleware'], function(){
// Web routes
});
Route::group(['prefix' => 'api'], function (){
});
class WebMiddleware {
public function handle($request)
{
\View::share('user', auth()->user());
}
}
PHP Snippet 14:
public function boot() {
View::share('youVarName', [1, 2, 3]);
}
PHP Snippet 15:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class ComposerServiceProvider extends ServiceProvider
{
/**
* Register bindings in the container.
*
* @return void
*/
public function boot()
{
// Using class based composers...
View::composer(
'profile', 'App\Http\ViewComposers\ProfileComposer'
);
// Using Closure based composers...
View::composer('dashboard', function ($view) {
//
});
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
//
}
}
PHP Snippet 16:
// Using class based composers...
View::composer(
'profile', 'App\Http\ViewComposers\ProfileComposer'
);
PHP Snippet 17:
class ProfileComposer
{
/**
* The user repository implementation.
*
* @var UserRepository
*/
protected $users;
/**
* Create a new profile composer.
*
* @param UserRepository $users
* @return void
*/
public function __construct(UserRepository $users)
{
// Dependencies automatically resolved by service container...
$this->users = $users;
}
/**
* Bind data to the view.
*
* @param View $view
* @return void
*/
public function compose(View $view)
{
$view->with('count', $this->users->count());
}
}
PHP Snippet 18:
{{ $count }}
PHP Snippet 19:
// Using class based composers...
View::composer(
'profile', 'App\Http\ViewComposers\ProfileComposer'
);
PHP Snippet 20:
// Using class based composers...
View::composer(
'*', 'App\Http\ViewComposers\ProfileComposer'
);
PHP Snippet 21:
public function boot()
{
view()->share('key', 'value');
}
PHP Snippet 22:
'aliases' => [
...,
'Helper' => App\HelperClass\Helper::class,
],
PHP Snippet 23:
namespace App\HelperClass;
class Helper
{
public static function Sample()
{
//Your Code Here
}
}
PHP Snippet 24:
<?php
return [
'versionNumber' => '122231',
];
PHP Snippet 25:
config('variable.versionNumber')
PHP Snippet 26:
// in boot function
view()->composer('*', function ($view) {
$data = User::messages();
$view->with('var_messages',$data);
});
PHP Snippet 27:
public static function messages(){ // this is just example
$my_id = auth()->user()->id;
$data= Message::whereTo($my_id)->whereIs_read('0')->get();
return $data; // return is required
}
PHP Snippet 28:
{{ $var_messages }}
PHP Snippet 29:
use Illuminate\Support\Facades\View;
use App\Product;
public function boot()
{
$products = Product::all();
View::share('products', $products);
}
PHP Snippet 30:
public function __construct()
{
$tables = DB::select('SHOW TABLES'); // returns an array of stdObjects
view()->share('tables', $tables);
}
PHP Snippet 31:
public function boot()
{
\View::composer('*', function($view){
$view->with('key', 'value');
});
}
PHP Snippet 32:
public function boot(){
//
View::composer('*', function($view){
view->with('role', Auth::user()->infouser->role);
});
}
PHP Snippet 33:
class Controller extends BaseController{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function __construct(){
$categories = Category::where('show_menu', '=', 1)->where('status', '=', 'approved')->get();
view()->share('categories', $categories);
}}