Symfony2 accessing values in base html - php

I have a dynamic menu and in the top right corner I want to dinamically display the number of products I have in my cart. All my twigs extend base.html.twig in which I have this dynamic menu.
<div id="mainBody" class="container">
<header id="header">
<div class="row">
<div class="span12">
<img src="{{ asset('bundles/mpshop/img/logo_title.png') }}" alt="Bootsshop"/>
<div class="pull-right"> <br/>
<span class="btn btn-mini btn-warning"> <i class="icon-shopping-cart icon-white"></i> [ {{ cart|length }} ] </span>
<span class="btn btn-mini active">$155.00</span>
<span class="btn btn-mini">£</span>
<span class="btn btn-mini">€</span>
</div>
</div>
</div>
However when I add {{ cart|length }} I am getting variable cart does not exist. Does base.html.twig has its own controller? How to give the base twig the cart with session so it could see my array?

One way to do this is create a helper class and include a method from that class in the menu (navbar?)
To do this you can make a regular Controller called for example HelperController.php then create a method within that new controller, like shownumberofproductsAction and then create a corresponding view file: shownumberofproducts.html.twig
Then in your top right menu simply call:
{% render controller("MySomethingBundle:Helper:shownumberofproducts") %}

Related

I need to find vueJs component/template file in laravel application

I am working on laravel application and trying to find where from these following variables are coming so that I would customize them; As I have searched and found that these variables are coming from vueJs component file, and could not find this vueJs file as I have searched whole application's file and I have zero knowledge in vueJs. Is vueJs file is also embedded in head or before body tag as we put css and js files? please help me.
<div class="row align-items-center" v-cloak>
<div :class="item.pulse"></div>
<div class="col ml--2">
<small> #{{ item.last_status }}</small><br />
<small> #{{ item.time }}</small>
<h4 class="mb-0">
#{{ item.id }} #{{ item.restaurant_name }}
</h4>
<small>#{{ item.client }}</small><br />
<small>#{{ item.price }}</small><br />
</div>
<div class="col-auto">
<a class="btn btn-sm btn-primary" :href="item.link">{{ __('Details')}}</a>
</div>
</div>

how to add button on .php file by editing it

{!! view_render_event('bagisto.shop.layout.header.account-item.before') !!}
<login-header></login-header>
{!! view_render_event('bagisto.shop.layout.header.account-item.after') !!}
<script type="text/x-template" id="login-header-template">
<div class="dropdown">
<div id="account">
<div class="welcome-content pull-right" #click="togglePopup">
<i class="material-icons align-vertical-top">perm_identity</i>
<span class="text-center">
#guest('customer')
{{ __('velocity::app.header.welcome-message', ['customer_name' => trans('velocity::app.header.guest')]) }}!
#endguest
#auth('customer')
{{ __('velocity::app.header.welcome-message', ['customer_name' => auth()->guard('customer')->user()->first_name]) }}
#endauth
</span>
<span class="select-icon rango-arrow-down"></span>
</div>
</div>
<div class="account-modal sensitive-modal hide mt5">
<!--Content-->
#guest('customer')
<div class="modal-content">
<!--Header-->
<div class="modal-header no-border pb0">
<label class="fs18 grey">{{ __('shop::app.header.title') }}</label>
<button type="button" class="close disable-box-shadow" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true" class="white-text fs20" #click="togglePopup">×</span>
</button>
</div>
<!--Body-->
<div class="pl10 fs14">
<p>{{ __('shop::app.header.dropdown-text') }}</p>
</div>
<!--Footer-->
<div class="modal-footer">
<div>
<a href="{{ route('customer.session.index') }}">
<button
type="button"
class="theme-btn fs14 fw6">
{{ __('shop::app.header.sign-in') }}
</button>
</a>
</div>
<div>
<a href="{{ route('customer.register.index') }}">
<button
type="button"
class="theme-btn fs14 fw6">
{{ __('shop::app.header.sign-up') }}
</button>
</a>
</div>
</div>
</div>
#endguest
#auth('customer')
<div class="modal-content customer-options">
<div class="customer-session">
<label class="">
{{ auth()->guard('customer')->user()->first_name }}
</label>
</div>
<ul type="none">
<li>
{{ __('shop::app.header.profile') }}
</li>
<li>
{{ __('velocity::app.shop.general.orders') }}
</li>
<li>
{{ __('shop::app.header.wishlist') }}
</li>
<li>
{{ __('velocity::app.customer.compare.text') }}
</li>
<li>
{{ __('shop::app.header.logout') }}
</li>
</ul>
</div>
#endauth
<!--/.Content-->
</div>
</div>
</script>
#push('scripts')
<script type="text/javascript">
Vue.component('login-header', {
template: '#login-header-template',
methods: {
togglePopup: function (event) {
let accountModal = this.$el.querySelector('.account-modal');
let modal = $('#cart-modal-content')[0];
if (modal)
modal.classList.add('hide');
accountModal.classList.toggle('hide');
event.stopPropagation();
}
}
})
</script>
#endpush
I have a php file for creating login page
it look like this page view
how to add a new login button like login with google by editing in it.
Please be detail while giving your answer, it is very helpful for those who are new in php.
It is better to provide general answers that is helpful for those who are working in other domain related to php.
This code belong to bagisto framework which is base on laravel.Bagisto is greate framework for creating ecommerce sites.
This is the repo of bagisto https://github.com/bagisto/bagisto.git
I am new to stackoverflow so feel free to improve my question for more clarity friends if need.
We have added social login in our master branch, you may take a pull from there. Below is the provided PR link
https://github.com/bagisto/bagisto/pull/3367
PS - as the user asked the questions regarding the platform bagisto that's why I am answering for that purpose.

Variable from a controller to a view without a foreach loop in Laravel

I want to show the content of an article created, I have a show method in my controller
public function show_capitulos($id)
{
$data=Capitulo::select('capitulos.titulo as capitulo','capitulos.descripcion','capitulo_secciones.contenido','capitulo_videos.video')
->join('capitulo_secciones','capitulos.id','=','capitulo_secciones.capitulo_id')
->join('capitulo_videos','capitulos.id','=','capitulo_secciones.capitulo_id')
->where('capitulos.id',$id);
return view('administrador.capitulos.show')->with(['data'=>$data]);
}
And I want to pass that data to my view in Laravel, without using a foreach loop, but it keeps showing the error of undefined variable
I've read that you use the get() function to retrieve a collection of data and that with first() you get only one.
**UPDATE:**After adding the get() in my code a new error is showing
error
This is what I have in my view
<div class="row" id="contenido-cursos">
<div class="justify-content-between flex-wrap align-items-center pb-2 mb-3 ">
<h2 class="title">Capítulo 1</h2>
<h1 class="nombre-capitulo"> {{ $data->capitulo }}</h1>
<div class="linea-capitulo"></div>
<p id="titulo-capitulo">{{$data->descripcion}}</p>
<h2 class="title sub">Contenido</h2>
<p class="contenidocap"><br>{{$data->contenido}}</p>
<a class="btn btn-theme btn-block title extras" href="#"><img src="{{asset('assets/img/recursos.png')}}"> {{ __('Descargar Cap1.pdf') }}</a>
<a class="btn btn-theme btn-block title extras" href="#"><img src="{{asset('assets/img/test.png')}}"> {{ __('Hacer Test 1') }}</a>
</div>
</div>
You are defining the query but not executing it.
After concatenating where() and join() you have to call get() to retrieve the results for the query.

using sidebar in different blades

I'm a newbie in laravel and actually I have to write the sidebar in every blade of the application to make it work, but I'd like using a different sidebar depending on the section of the site where I am.
So this is what I'm trying to do:
EDIT 1
layouts/main.blade.php
<div class="wrapper">
<div class="sidebar" data-color="brown" data-active-color="danger">
<div class="logo">
<!-- Content -->
</div>
<!-- Sidebar -->
#if(request()->is("{{ url('/')}}/{operator}"))
#include('operator.sidebar')
#else
#include('stduser.sidebar')
#endif
<!-- End sidebar -->
</div>
<div class="main-panel">
<!-- Navbar -->
<nav></nav>
<!-- End navbar -->
<!-- Main content section -->
#yield('main-panel')
<!-- End main content section -->
<!-- Footer -->
<footer></footer>
<!-- End footer -->
</div>
</div>
stduser/dashboard.blade.php
#extends('layouts.main')
#section('main-panel')
<!-- Main panel contents -->
#endsection
#section('extrajs')
<!-- script contents -->
#endsection
stduser/sidebar.blade.php
<div class="sidebar-wrapper">
<div class="user btn-rotate">
<div class="photo">
<i class="fa fa-user-circle-o fa-2x" aria-hidden="true" style="color:#fff"></i>
</div>
<div class="info">
<a href="{{ url('/user') }}/profile">
<span>
{{ Auth::user()->name }}
</span>
</a>
<div class="clearfix"></div>
</div>
</div>
<ul class="nav">
<li class="active btn-rotate">
<a href="{{ url('/') }}">
<i class="nc-icon nc-bank"></i>
<p>Companies</p>
</a>
</li>
</ul>
</div>
operator/sidebar.blade.php
<div class="sidebar-wrapper">
<div class="user btn-rotate">
<div class="photo">
<i class="fa fa-user-circle-o fa-2x" aria-hidden="true" style="color:#fff"></i>
</div>
<div class="info">
<a href="{{ url('/user') }}/profile">
<span>
{{ Auth::user()->name }}
</span>
</a>
<div class="clearfix"></div>
</div>
</div>
<ul class="nav">
<li class="active btn-rotate">
<a href="{{ url('/') }}/{{ $operator->id }}/about">
<i class="fa fa-tachometer" aria-hidden="true"></i>
<p>DashBoard</p>
</a>
</li>
<li class="btn-rotate">
<a href="{{ url('/')}}/{{ $operator->id}}/suppliers">
<i class="fa fa-link" aria-hidden="true"></i>
<p>Suppliers</p>
</a>
</li>
<li class="btn-rotate">
<a href="{{ url('/')}}/{{ $operator->id}}/products">
<i class="fa fa-product-hunt" aria-hidden="true"></i>
<p>Products</p>
</a>
</li>
</ul>
</div>
This is how my views are structured:
Is there a way to make it work?
you can include the blade file like so #include('layouts/sidebar_' . $sidebarName) and if you want to avoid errors when include doesnt exist you can use #includeIf('view.name', ['some' => 'data'])
So you have just the include statement and the sidebar content only once
Based on your feedback to addi2113's answer, it seems like you're wanting to switch out the sidebar include based on which page you're on. There are several ways to do this. The simplest (yet least flexible) way to do this would be to show a certain sidebar based on the route. For instance, if you have a predictable route structure for all "operator" pages, such as example.com/operator/*, you could do the following by using an #if statement in your blade view. Like this:
#if(request()->is("/unique/url/pattern"))
#include('operator.sidebar')
#else
#include('stduser.sidebar')
#endif
Obviously, you can edit this to use any logic you want, but this is a somewhat simple way to handle this.
EDIT: Try this in your main.blade instead of using a section and yield:
<div class="wrapper">
<div class="sidebar" data-color="brown" data-active-color="danger">
<div class="logo">
<!-- Content -->
</div>
<!-- Sidebar -->
<div class="sidebar-wrapper">
<div class="user btn-rotate">
<div class="photo">
<i class="fa fa-user-circle-o fa-2x" aria-hidden="true" style="color:#fff"></i>
</div>
<div class="info">
<a href="{{ url('/') }}/profile">
<span>
{{ Auth::user()->name }}
</span>
</a>
<div class="clearfix"></div>
</div>
</div>
#if(request()->is('/unique/url/pattern'))
#include('operator.sidebar')
#else
#include('stduser.sidebar')
#endif
</div>
</div>
</div>
EDIT 2:
Since it appears you are using a dynamic URL for the operator pages, you have two options. The first option is to make your operator routes more unique than they currently are so that you can use an asterisk to denote all routes of a current pattern. For instance, in routes/web.php, change your routes for operator pages to this type of pattern:
Route::get('/operator/{operator}/about','OperatorController#about')->name('operator-about');
By adding the operator slug into the url, you now have a UNIQUE path that you can reference. Then, in your main blade, you would reference all of the operator routes together like this:
#if(request()->is('/operator/*'))
#include('operator.sidebar')
By making the URL unique, you have made a very simple way to reference all routes where you want to show the operator sidebar.
Another option, which is not as robust in my opinion, is to refer to the specific routes by naming. For instance, using the route I defined up above with the name of "operator-about", I could show the operator sidebar like this:
#if(Route::currentRouteName()=="operator-about")
#include('operator.sidebar')
You would then expand upon this by explicitly defining all named routes that you would want to show the operator sidebar for. As you can probably tell, this will get messy if there are a lot of routes you want to include. I don't recommend to do it this way, but you can feel free to solve the problem however you want.

How to Route back with passing parameters using Laravel?

I am working on Laravel 5.0. I want to return back from my current page to the previous page with passing parameter. I have an id of all images and galleries in my urls. After clicking on a particular image, i move to another page with the id of that image in my url, Now, i want to return back to my gallery page to which that image belongs to.
My controller:
public function viewgallerypics($id){
$gallery= Gallery::findorFail($id);
return view('dropzone' , ['gallery'=>$gallery]);
}
public function bigimage($id){
$gallery=Gallery::all();
$image=image::findorFail($id);
return view('bigimage' , ['image'=>$image,'gallery'=>$gallery]);
}
My routes:
Route::get('/image/big/{id}' ,[
'uses'=>'GalleryController#bigimage',
'as'=>'bigimage'
]);
Route::get('/gallery/view/{id}' ,[
'uses'=>'GalleryController#viewgallerypics',
'as'=>'viewpics'
]);
My view:
<section class="col-md-1">
<a class="btn btn-primary btn-lg"
href="HERE, WHAT I HAVE TO PASS TO GET MY DESIRED
PAGE????">Back</a>
</section>
My desired page where i want to return back depending on the id pass through the route:
<div class="row">
<div class="col-md-offset-1 col-md-10">
<div id="gallery-images">
<ul>
#foreach($gallery->images as $image)
<li>
<a href="{{ URL('/image/big/'.$image->id) }}">
<img id="jumboimage2" src="{{ url($image->file_path) }}"></a>
</li>
<li>
<a href="{{ URL('/image/delete/'.$image->id) }}" id="margin">
<span id="margin" class="glyphicon glyphicon-remove-sign"></span></a>
</li>
#endforeach
</ul>
</div>
</div>
</div>
use {{ URL::previous() }}
<section class="col-md-1">
<a class="btn btn-primary btn-lg" href="{{ URL::previous() }}">Back</a>
</section>

Categories