Creation and data transfer of Larave view
Create View
<!-- This view stores resources/views/greeting. blade. php --> <html> <body> <h1>Hello, {{ $name }}</h1> </body> </html>
Route::get('/', function () { Return view ('greeting ', ['name'=>'Scholar']); });
return view('admin.profile', $data);
use Illuminate\Support\Facades\View; if (View::exists('emails.customer')) { // }
use Illuminate\Support\Facades\View; return View::first(['custom.admin', 'admin'], $data);
Transfer Data to View
Return view ('greetings', ['name '=>' Scholar ']);
$view=view ('greeting ') ->with ('name', 'scholar');
<? php namespace App\Providers; use View; class AppServiceProvider extends ServiceProvider { /** *Start all application services * * @return void */ public function boot() { View::share('key', 'value'); } /** *Registered Service Provider * * @return void */ public function register() { // } }
View Composer
<? php namespace App\Providers; use Illuminate\Support\Facades\View; use Illuminate\Support\ServiceProvider; class ComposerServiceProvider extends ServiceProvider { /** *Register the binding in the container * * @return void * @author https://xueyuanjun.com */ public function boot() { //Using Composers Based on Class Methods View::composer( 'profile', 'App\Http\ViewComposers\ProfileComposer' ); //Using Composers Based on Callback Functions View::composer('dashboard', function ($view) {}); } /** *Registered Service Provider * * @return void */ public function register() { // } }
Note: If you create a new service provider to include the view Composer registration, you need to add the service provider to the configuration file
config/app.php Of
providers Array.
<? php namespace App\Http\ViewComposers; use Illuminate\View\View; use Illuminate\Repositories\UserRepository; class ProfileComposer { /** *User warehouse implementation * * @var UserRepository */ protected $users; /** *Create a new attribute composer * * @param UserRepository $users * @return void */ public function __construct(UserRepository $users) { //Dependency injection is automatically resolved through the 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()); } }
Note: All view Composers are resolved through the service container, so you can declare any dependency you need in the constructor of Composer class.
View::composer( ['profile', 'dashboard'], 'App\Http\ViewComposers\MyViewComposer' );
View::composer('*', function ($view) { // });
View::creator('profile', 'App\Http\ViewCreators\ProfileCreator');