I'm trying to utilize the Laravel landing page route. For example Laravel takes you to the welcome page by default, and it has no url text after the slash.
Route::get('/', function () {
return view('welcome');
});
In my welcome page I'm using following condition to apply styling based on the route name.
{!! Route::is('/')? 'class="index"':'' !!}
However this code doesn't work. How can I check the route of the welcome page properly?
Edit: Using "Request" instead of "Route" makes it work. However for consistency's sake I would like to know if it can be done using "Route" too.
Name your route and use this name to condition your styling
Route::get('/', function () {
return view('welcome');
})->name('home');
In your blade
{!! (Route::currentRouteName() == 'home')? 'class="index"':'' !!}
Try this:
<li class="#if (request()->is('/')) index #endif"></li>
// ...
</li>
You can use wildcards as well with this:
<li class="#if (request()->is('/some-url/*')) active #endif">
// ...
</li>
try this
#if(Request::is('/'))
class="index"
#endif
or more simply
if(Request::is('/')) {
class="index"
}
Try this:
<li {!! Request::is('/') ? 'class=index' : '' !!}>...</li>
Route::get('/', function () {
return view('welcome');
})->name('home');
in blade
class = "default class #if(\Request::route()->getName() == 'home')your_class #endif"
Related
I registered following routes on my web.php file
Route::domain('{domain}.project.test')->group(function () {
Route::get('/', function ($domain) {
return view('welcome',compact('domain'));
});
Auth::routes(['verify' => true]);
Route::get('/home', 'HomeController#index')->name('home')->middleware('verified');
});
I had to pass the domain variable into welcome.blade.php in order to pass it into route() method
<div class="top-right links">
#auth
Home
#else
Login
Register
#endauth
</div>
is there any way to pass$domain variable on to the
route() method by default
You can write a custom helper function, something like droute() below:
if (! function_exists('droute')) {
function droute($name, $parameters = [], $absolute = true)
{
$parameters['domain'] = app('request')->route('domain');
return app('url')->route($name, $parameters, $absolute);
}
}
Here is a link on how to create custom helpers.
Hello guys I made a CRUD(just create and read ) in Laravel project ,the create is working good and i passed the "loan" in show function but in the view it says undefined variable:
my show function in controller:
public function show($id)
{
$loan=loan::find($id);
return view('finance/loans')->with('loan',$loan);
}
My routes:
Auth::routes();
Route::get('/loans', 'LoanController#viewLoanForm');
Route::post('/loans', 'LoanController#store');
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('loan','loanController');
Route::get('finance/loans', function () {
return view('loans');
});
And here is where i call the variable loan in the view:
#extends('layouts.app')
#section('content')
<link href="{{ asset('css/loans.css') }}" rel="stylesheet">
<html>
<div class="container">
...
</div>
</div>
</div>
<div class="row">
<h1>{{$loan->nume}}</h1>//"nume"is the row from db i want to show
</div>
</div>
</html>
#endsection
The ERROR
You misspelled the route!
Your request does not reach the controller method
Route::get('finance/loans', function () {
return view('loans');
});
should be so
Route::get('finance/loans/{$id}','loanController#show');
or should use the controller resource route
Route::resource('loans','loanController');
url 'loans/show/{$loan_id}'
Show Loan
your return should be like this:
return view('finance/loans')->with(['loan'=>$loan]);
instead of
return view('finance/loans')->with('loan',$loan);
I think you need to on the route::resource('finance/loans') instead of ('loans').
And will work for sure
Use this :
return view('finance.loans',compact(['loan']));
my blade looks like this...
#if(count($alleSpiele) > 0)
<ul class="list-group">
#foreach($alleSpiele as $alleSpieleOutput)
{{$alleSpieleOutput->heimmannschaft}}
#endforeach
</ul>
#endif
This is my route...
Route::get('/spielerAuswahl', 'SpielplanController#getHeimGast');
Here is my controller...
public function getHeimGast(){
return view('test');
}
Now, my problem is to take the choosen variable from a href heimmannschaft from blade to route into the controller? What is the correct way to do this?
change your links to this
{{$alleSpieleOutput->heimmannschaft}}
then change your route to
Route::get('/spielerAuswahl/{value}', 'SpielplanController#getHeimGast');
then in the controller you can have the variable like this:
public function getHeimGast($value){
// use $value in your code
return view('test');
}
This is code snippet from my view file.
#foreach($infolist as $info)
{{$info->prisw}} / {{$info->secsw}}
#endforeach
Here is my route which I defined inside route file
Route::get('switchinfo','SwitchinfoController');
I want to pass two values inside href tag to above route and retrieve them in controller. Can someone provide code to do this thing?
Since you are trying to pass two parameters to your controller,
You controller could look like this:
<?php namespace App\Http\Controllers;
class SwitchinfoController extends Controller{
public function switchInfo($prisw, $secsw){
//do stuffs here with $prisw and $secsw
}
}
Your router could look like this
$router->get('/switchinfo/{prisw}/{secsw}',[
'uses' => 'SwitchinfoController#switchInfo',
'as' => 'switch'
]);
Then in your Blade
#foreach($infolist as $info)
Link
#endforeach
Name your route:
Route::get('switchinfo/{parameter}', [
'as'=> 'test',
'uses'=>'SwitchinfoController#function'
]);
Pass an array with the parameters you want
<a href="{{route('test', ['parameter' => 1])}}">
{{$info->prisw}} / {{$info->secsw}}
</a>
and in controller function use
function ($parameter) {
// do stuff
}
Or if don't want to bind parameter to url and want just $_GET parameter like url/?parameter=1
You may use it like this
Route::get('switchinfo', [
'as'=> 'test',
'uses'=>'SwitchinfoController#function'
]);
function (){
Input::get('parameter');
}
Docs
You can simply pass parameter in your url like
#foreach($infolist as $info)
<a href="{{ url('switchinfo/'.$info->prisw.'/'.$info->secsw.'/') }}">
{{$info->prisw}} / {{$info->secsw}}
</a>
#endforeach
and route
Route::get('switchinfo/{prisw}/{secsw}', 'SwitchinfoController#functionname');
and function in controller
public functionname($prisw, $secsw){
// your code here
}
In Twig partial rendered by separate controller, I want to check if current main route equals to compared route, so I can mark list item as active.
How can I do that? Trying to get current route in BarController like:
$route = $request->get('_route');
returns null.
Uri is also not what I'm looking for, as calling below code in bar's twig:
app.request.uri
returns route similar to: localhost/_fragment?path=path_to_bar_route
Full example
Main Controller:
FooController extends Controller{
public function fooAction(){}
}
fooAction twig:
...some stuff...
{{ render(controller('FooBundle:Bar:bar')) }}
...some stuff...
Bar controller:
BarController extends Controller{
public function barAction(){}
}
barAction twig:
<ul>
<li class="{{ (item1route == currentroute) ? 'active' : ''}}">
Item 1
</li>
<li class="{{ (item2route == currentroute) ? 'active' : ''}}">
Item 2
</li>
<li class="{{ (item3route == currentroute) ? 'active' : ''}}">
Item 3
</li>
</ul>
pabgaran's solution should work. However, the original problem occurs probably because of the request_stack.
http://symfony.com/blog/new-in-symfony-2-4-the-request-stack
Since you are in a subrequest, you should be able to get top-level (master) Request and get _route. Something like this:
public function barAction(Request $request) {
$stack = $this->get('request_stack');
$masterRequest = $stack->getMasterRequest();
$currentRoute = $masterRequest->get('_route');
...
return $this->render('Template', array('current_route' => $currentRoute );
}
Haven't run this but it should work...
I think that the best solution in your case is past the current main route in the render:
{{ render(controller('FooBundle:Bar:bar', {'current_route' : app.request.uri})) }}
Next, return it in the response:
public function barAction(Request $request) {
...
return $this->render('Template', array('current_route' => $request->query->get('current_route'));
}
And in your template compares with the received value.
Otherwise, maybe is better to use a include instead a render, if you don't need extra logic for the partial.
in twig you can send request object from main controller to sub-controller as parameter:
{{ render(controller('FooBundle:Bar:bar', {'request' : app.request})) }}
in sub-controller:
BarController extends Controller{
public function barAction(Request $request){
// here you can use request object as regular
$country = $request->attributes->get('route_country');
}
}