Passing URL to Views in Laravel - php

Here is my controller code:
$view = View::make('homepage');
$loglink = URL::route('account.login');
$view->loglink = $loglink;
return $view;
Here is how I recv it in view:
<a href="{{ $loglink }}" class="red">
I get an error saying:
Undefined variable: loglink (View:
C:\BottleBookings\server\app\views\partials\header.blade.php)
Any problem with the way I am sending or recving it?

Thats not how view creation works. Just return your view and pass the variables you want to use with the with() function in the same command. Create your view like this:
$loglink = URL::route('account.login');
return View::make('homepage')->with('loglink', $loglink);
Or even shorter:
return View::make('homepage')->with('loglink', URL::route('account.login'));
Also, you don't really have to pass the link URLs, it's perfectly fine to generate them where they are needed in the view:
<a href="{{ URL::route('account.login') }}" class="red">

why you complicate. just put the datas in array and pass it to view.
Controller
$data['loglink'] = URL::route('account.login');
return View::make('homepage',$data);
p.s. your loglink variable will be available to only homepage view. NOT on header page.
if you want to pass the loglink to header, then you have to pass it as View::composer or View::share or where you are including the file. any one of the three will do.

Related

Sharing variable to laravel layouts

I need to share variable to standart layout.app, but when i try to do it-the variable in blade is undefined.
There is the controller part:
public function SidebarOffice(){
$facultys=Faculty::all();
return view('layouts.app', compact('facultys'));
}
And the layout.app part
#foreach($facultys as $faculty)
<a class="dropdown-item" href="#">{{$faculty->id}}</a>
#endforeach
I also saw a way with a View::share. Can it work out with him?
Trying doing it with 'with' method.
It should look something like this:
return view('layouts.app')->with(compact('facultys')));
return view('layouts.app',['facultys'=>$facultys]);

Laravel. conflict with routes

I have a problemwith my routes. When I call 'editPolicy' I dont know what execute but is not method editPolicy. I think I have got problem beteweeb this two routes:
My web.php ##
Route::get('admin/edit/{user_id}', 'PolicyController#listPolicy')->name('listPolicy');
Route::put('/admin/edit/{policy_id}','PolicyController#editPolicy')->name('editPolicy');
I call listPolicy route in all.blade.php view like this:
{{ $user->name }}
And call editPolicy route in edit.blade.php view like this:
Remove</td>
My PolicyController.php is:
public function listPolicy($user_id)
{
$policies = Policy::where('user_id', $user_id)->get();
return view('admin/edit',compact('policies'));
}
public function editPolicy($policy_id)
{
dd($policy_id);
}
But I dont know what happend when I call editPolicy route but editPolicy method not executing.
Any help please?
Best regards
Clicking an anchor will always trigger a GET request.
route('listPolicy', $user->id) and route('editPolicy', $policy->id) will both return admin/edit/{an_id} so when you click your anchor, listPolicy will be executed. If you want to call editPolicy, you have to send a PUT request via a form, as defined when you declared your route with Route::put.
Quick note, your two routes have the same URL but seem to do very different things, you should differentiate them to avoid disarray. It's ok to have multiple routes with the same url if they have an impact on the same resource and different methods. For example for showing, deleting or updating the same resource.
Have a look at the documentation.

Symfony2 app.request.get return null in rendered twig template

I render my header.twig from the base.twig file via the render function. So in my base.twig there is the following code to trigger header controller:
{{ render(controller('MyBundle:Global:header')) }}
That controller renders the header.twig. In this twig file there the the following code link for changing the language:
<img src="{{ asset('flags/'~app.request.locale~'.png', 'img') }}" />
The objects form app.request.get('_route') and app.request.get('_route_params') both return null.
app.request.get('_route')
If I run the same code link directly in the base.twig the request returns the correct objects. Looks like because the header.twig has it own controller the request are not working. Is it possible to request the route and parametsr of the active url in a other way?
Adding to #Gustek's answer because I missed the actual issue in the first case.
You are actually rendering a sub request so the _route and _route_params are from the sub request rather than the main request. As you are using a controller the cleanest approach would be to get the parameters from the master request and pass those in as parameters in the controller call. You could pass them in through the render call (as #Gustek has answered) but that would mean you would mean to do it with every call, or you could pass the request stack into the twig session but that would be unnecessary extra work.
public function headerAction(Request $request)
{
$masterRequest = $this->container->get('request_stack')->getMasterRequest();
//...
return $this->render('MyBundle:Global:header.html.twig', [
//...
'_route' => $masterRequest->attributes->get('_route'),
'_route_params' => $masterRequest->attributes->get('_route_params'),
]);
}
controller method takes 2 optional arguments.
http://symfony.com/doc/current/reference/twig_reference.html#controller
Not 100% sure about it but maybe this will work:
{{ render(controller('MyBundle:Global:header',
{
'_route': app.request.get('_route'),
'_route_params': app.request.get('_route_params')
}
)) }}

How to access cookie from view in PHP Laravel 5

I can access cookie in controller then pass it to view
//HomeController.php
public function index(Request $request)
{
$name = Cookie::get('name');
return view('index', ['name'=> $name]);
}
But I want to write a small control (widget) that can fetch data from cookie without concern of parent controller. For example, header, footer widgets could fetch its own data without main page controller knowing which data is needed.
I can query the data from database by using View Composer. But, how can I access data from view in the request cookie ?
Using static function with defining namespace and etc is a not safe.
Cuz maybe in next versions of framework this namespace can change.
It's better to use helper functions.
{{ request()->cookie('laravel_session') }}
or
{{ cookie('laravel_session') }}
Tested on working app with Laravel 5.2
You can use {{ Cookie::get('laravel_session') }} to print out the cookie inside your view.
You can use
{{\Illuminate\Support\Facades\Cookie::get('laravel_session')}}
in a blade template
You can use:
$response = new \Illuminate\Http\Response(view('your_view'));
$response->withCookie(cookie('cookieName' , 'cookieValue' , expire));
return $response;
or
\Cookie::queue('cookieName', 'cookieValue', expire);
return view('your_view');
I've used both of them.

go to a new link in Laravel 4

suppose I want to go a new link like www.facebook.com. How do I do this in laravel 4. If i use return Redirect::to() then I get the error that
there is no method called www.facebook.com
in my controller. If I use header('Location: ' . $url); then I get the error
Undefined variable: content
since there is a code in my controller which says protected $layout = 'sites.master' in in the layouts/master file there is a {{$content}}
You can use the URL::route('named_route')') function inside the href attribute in the blade syntax to go to a particular named route and URL::to('link_adress') function inside the href attribute in the blade syntax to go a particular custom address.
Example code for creating a link to google would be like:
<a class="link" href="{{ URL::to('http://www.google.com') }}">Go to google</a>
The view should use the blade templating engine for this to work.

Categories