Laravel Blade how to pass $attributes to component from controller - laravel

I have a blade component containing $attributes; It would be a bag of attributes when the component called from another blade template but when from the controller via view() the $attributes is undefined! How can I pass data as $attributes from the controller?
Component: sample
<div {{ $attributes->except('content') }}>{{ $content }}</div>
Template: works well.
...
<x-sample class="test" content="test"/>
...
Controller: Error Undefined variable $attributes
$attributes = ['class' => 'test', 'content' => 'test'];
view('components.sample', $attributes)->render();
view('components.sample', ['attributes' => $attributes])->render();
view('components.sample')->with($attributes)->render();
UPDATE (Solution):
It works:
view('components.sample', [
'prop1' => '...',
'prop2' => '...',
'attributes' => new ComponentAttributeBag(['attr1' => '...', 'attr2' => '...']),
])->render();

what is happening is components inherit variable from view, if you define your variable from home view for example, it should work in component from that view. Then when you include the view to about page the variables wont be recognized since they are not inherited from about view. And in Laravel you cannot pass data directly form controller to component. but laravel have solved it by View Composer.
Since I dont like too much complication especially with providers I edited my AppServiceProvider you can create your own provider.
in YourServiceProvider in my case AppServiceProvider in boot() function there are three options you can use one. I recommend there 3rd one since its clean
public function boot()
{
// option1 - Every single view
View::share('shops', Shop::orderBy('name')->get());
// option2 - Gradular views with wildcards
View::composer(['client.*'], function ($view){
$view->with('shops', Shop::orderBy('name')->get());
});
// option3 - Dedicated class
View::composer(['client.includes.*','client.create-product','client.cart'], ShopsComposer::class);
View::composer(['client.includes.*','client.cart'], CartComposer::class);
}
If you use the 3rd method you have to create ViewComposer class
<?php
namespace App\Http\View\Composers;
use App\Models\CartItem;
use Illuminate\Support\Facades\Session;
use Illuminate\View\View;
class ShopsComposer
{
public function compose(View $view){
$shop = auth()->user()->shops->sortBy('name');
$cartItem = new CartItem();
$cartCount = 0;
if (Session::has('cartId')) {
$carts = Session::get('cartId');
$cartItems = $cartItem->with('products')->where('cart_id', '=', $carts->id)->get();
foreach ($cartItems as $item) {
$cartCount += $item->quantity;
}
}
$view->with('shopsComposer', $shop)->with('cartCount', $cartCount);
}
}
The variables you define there will be available to all the views. that includes components. Since component inherit variable from view file.
I am sorry I had to share my working example, since I am running out of time. I hope it works if I understood well your question.

Related

How to declare global variables available to all Blade Files? [duplicate]

This question already has answers here:
Laravel 5 - global Blade view variable available in all templates
(9 answers)
Closed 1 year ago.
I have products in a database that are essentially used on every single page. Instead of having to query the database, something like this:
$products = DB::table("products")->get();
And then passing it into view:
return view("site.products", array(
'products' => $products,
));
I don't want to do this for every view. Instead, I want $products to be available to ALL templates by default... so that I can do this:
#foreach ($products as $product)
... etc
How would I declare it in a global way to achieve this?
You can add below code in the boot method of AppServiceProvider
public function boot()
{
if (!$this->app->runningInConsole()) {
$products = DB::table("products")->get();
\View::share('products', $products);
}
}
Read more from here
A recommended way of doing this is to add a middleware that you apply to all the routes that you want to affect.
Middleware
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\View;
class GlobalVariablesMiddleware
{
$myVariable = "Value For Everyone";
View::share(['globalValue' => $myVariable]);
}
Add it to your kernel.php in the Http folder
protected $routeMiddleware = [
...
'myMiddleware' => \App\Http\Middleware\ GlobalVariablesMiddleware::class,
];
Once this is setup, you can easily apply it to individual routes or grouped ones to achieve what you are looking for
// Routes that will have the middleware
Route::middleware(['myMiddleware'])->group(function () {
// My first route that will have the global value
Route::resource('/profile', App\Http\Controllers\ProfileController::class);
// My second route that will have the global value
Route::resource('/posts', App\Http\Controllers\PostController::class);
});
By doing it this way, you can easily control the data in the future if you would chose not to have the data global.

Integrate Twig with CodeIgniter 4

I used Twig with Symfony and I really loved it. I now have a CodeIgniter project and I want to integrate Twig with it.
I installed the latest versions of CodeIgniter and Twig via Composer and and followed this tutorial but I believe the code in the tutorial is for CI v3.
Could anyone who has integrated Twig with CI v4 help me with the proper code please.
UPDATE
solution below!
Try this I hope it will help you
Install Composer and run the following command to get the latest version:
composer require "twig/twig:^3.0"
Then after installation add this line of code to the baseController initController method just after the parent::initController, just like the code below
namespace App\Controllers;
use CodeIgniter\Controller;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
class BaseController extends Controller
{
protected $helpers = [];
protected $twig;
// protected $helper = [];
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
$appPaths = new \Config\Paths();
$appViewPaths = $appPaths->viewDirectory;
$loader = new \Twig\Loader\FilesystemLoader($appViewPaths);
$this->twig = new \Twig\Environment($loader, [
'cache' => WRITEPATH.'/cache/twig',
]);
}
}
So with this now you can call the view files in other controllers extends to parent controller BaseController
e.g
namespace App\Controllers;
class Home extends BaseController
{
public function index ()
{
// To load a template from a Twig environment, call the load() method which returns a \Twig\TemplateWrapper instance:
$template = $this->twig->load('index.html');
// To render the template with some variables, call the render() method:
return $template->render(['the' => 'variables', 'go' => 'here']);
// The display() method is a shortcut to output the rendered template.
// OR You can also load and render the template in one fell swoop:
return $this->twig->render('index.html', ['the' => 'variables', 'go' => 'here']);
// If a template defines blocks, they can be rendered individually via the renderBlock() call:
return $template->renderBlock('block_name', ['the' => 'variables', 'go' => 'here']);
// Note any of them above will work
}
}
If you still want to use view() with twig like codeigniter 4 default view function you can modify the Common.php file in app directory
by adding this block of code below.
if (!function_exists('view'))
{
function view($tpl, $data = []) {
$appPaths = new \Config\Paths();
$appViewPaths = $appPaths->viewDirectory;
$loader = new \Twig\Loader\FilesystemLoader($appViewPaths);
$twig = new \Twig\Environment($loader, [
'cache' => WRITEPATH.'/cache/twig',
]);
if (!stripos($tpl, '.twig')) {
$tpl = $tpl . '.twig';
}
return $twig->render($tpl, $data);
}
}
Then in controller call it like this
return view('index', ['name' => 'Chibueze Agwu'])
Then in view file index.twig
<!DOCTYPE html>
<html>
<head>
<title>My Webpage</title>
</head>
<body>
<h1>My Webpage</h1>
{{ name }}
</body>
</html>
This will output
My Webpage
Chibueze Agwu
I haven't test this code but I hope it will work. If not call my attentions.
In order to obey the the rule of DRY (DO NOT REPEAT YOURSELF), you can go ahead to improve the code I will do that later
I found the solution some time ago and I'm posting it in case some people stumble across the question.
First of all, all your controllers must extend BaseController; this controller is available by default when you install CodeIgniter 4.
Create a custom helper file and put in [project-name]/appstarter/app/Helpers.
IMPORTANT
the name of your helper must be [name]_helper.php or it will not work!
for example mine is called custom_helper.php
Create the following function in the custom helper you just created:
use Twig\Environment;
use Twig\Extension\DebugExtension;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFilter;
if (!function_exists('twig_conf')) {
function twig_conf() {
// the follwing line of code is the only one needed to make Twig work
// the lines of code that follow are optional
$loader = new FilesystemLoader('Views', '../app/');
// to be able to use the 'dump' function in twig files
$twig = new Environment($loader, ['debug' => true]);
$twig->addExtension(new DebugExtension());
// twig lets you create custom filters
$filter = new TwigFilter('_base_url', function ($asset) {
return base_url() . '/' . $asset;
});
$twig->addFilter($filter);
return $twig;
}
}
NOTE
before creating any custom filter, make sure Twig doesn't already has one built-in.
Now in the BaseController you'll find an empty array called $helpers. You must put the name of your custom helper in it. Mine is called custom_helper.php; so the code looks like this for me:
protected $helpers = ['custom'];
Just below the array you'll find the constructor for BaseController and this is where the Twig library will be initialized; by calling the function you created in your custom helper:
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) {
parent::initController($request, $response, $logger);
$this->twig = twig_conf();
}
Now you are good to go! To render your twig files in any controller:
return $this->twig->render('twig_name', $dataArray);
Try this I hope it will help you.
Install Composer and run the following command to get the latest version:
composer require "twig/twig:^3.0"

Laravel phpunit testing controller which returns view with data

Am trying to adapt TDD on laravel, but am getting trouble to test controller which queries a collection of records and returns view with data when I run my test it returns an error message that trying to get the id of undefined property
// my test
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
class JobApplicationTest extends TestCase
{
use WithoutMiddleware;
/** #test */
public function user_can_see_all_open_positions()
{
// when user visit apply for a job page
$this->withoutExceptionHandling();
$response = $this->get(route('positions.open'));
$response->assertSuccessful();
$response->assertViewIs('positions.open.index');
$response->assertViewHas('positions');
}
}
// My controller
public function open(Request $request)
{
//TODO support additional filters & searches
//initialize query
$query = Position::query()->which()->are()->open();
//paginate query result
$positions = $query->paginate(config('app.defaults.pageSize'));
$data = [
'route_title' => 'Open Job Positions',
'route_description' => 'Available Job Positions',
'positions' => $positions,
];
return view('positions.open.index', $data);
}
so the problem is when I run text it looks view is returned with no data while view references data returned to the controller
error message when running test
1) Tests\Unit\JobApplicationTest::user_can_see_all_open_positions
ErrorException: Trying to get property 'id' of non-object (View:
/var/www/html/ipf-projects/niajiri/resources/views/pos
itions/open/index.blade.php)
<h3>
<a href="{{ route('positions.preview', [
'id' => $position->id,
'slug' => str_slug($position->title)
]) }}" class="text-navy">
{!! $position->title !!} - {!! $position->organization->name !!}
</a>
</h3>
Within 'positions.open.index' it is trying to access an id property on a non-object.
This could be because your $query does not return any Position objects (i.e. $data->positions returns an empty Collection)
When running unit tests it is likely that it is creating a temporary database (sqlite by default) which will not be populated with any data. Try adding some Positions in your test like so:
/** #test */
public function user_can_see_all_open_positions()
{
// Create Positions
Position::create([/* Some data here */]);
Position::create([/* Some data here */]);
// when user visit apply for a job page
$this->withoutExceptionHandling();
$response = $this->get(route('positions.open'));
$response->assertSuccessful();
$response->assertViewIs('positions.open.index');
$response->assertViewHas('positions');
}

Laravel 5.6 Function () does not exist in route/web.php

This is my code using to send an email
Route::post('/mail/send', [
'EmailController#send',
]);
in EmailController this is the send action
public function send(Request $request)
{
$data = $request->all();
$data['email'] = Input::get('email');
$data['name'] = Input::get('name');
$obj = new \stdClass();
$obj->attr = 'Hello';
Mail::to("dev#mail.com")->send(new WelcomeEmail($obj));
}
getting a error as Function () does not exist
In your route/web.php file
Change it to
Route::post('/mail/send', 'EmailController#send');
Refer to the documentation to see the possible options to define routes:
https://laravel.com/docs/5.6/routing
Route's action method can be defined using a array, but not simply wrap controller#action in an array, you should assign it to array's key 'uses'.
In your example, it should be like this:
Route::post('/mail/send', [
'uses' => 'EmailController#send',
//'middleware' => .... assign a middleware to this route, if needed
]);
the array form usually is used when we want to specify more specification about the route like use a specific middleware and pass middleware parameters.
if you just want to define route's processing method you can simply use controller#action as Route::post's second parameter:
Route::post('/mail/send','EmailController#send');
In your route ...
Route::post('/mail/send','EmailController#send')->name('send_email');
Inside of your HTML form add below code...
<form action="{{route('send_email')}}" method="post">
...
{{csrf_field()}}

Laravel undefine variable in view

I'm new to laravel. Using version 5.4 and tried to search but don't see what I'm doing wrong. I keep getting an "Undefined variable: post" in my view. I'm also doing form model binding. Model binding works properly when manually entering URL. Just can't click on link to bring up edit view.
My routes:
Route::get('test/{id}/edit','TestController#edit');
My controller:
public function edit($id)
{
$post = Post::find($id);
if(!$post)
abort(404);
return view('test/edit')->with('test', $post);
}
My form:
{{ Form::model($post, array('route' => array('test.update', $post->id), 'files' => true, 'method' => 'PUT')) }}
You're assigning the post value to 'test', so should be accessible with $test rather than $post.
You probably want to do either of these two things instead:
return view('test/edit')->with('post', $post);
or
return view('test/edit', ['post' => $post]);
https://laravel.com/docs/5.4/views
Your controller is sending a variable named "test", but your error says that your blade file doesn't have the $post variable passed into it. This can be fixed by changing "test" to "post" in your controller.

Resources