Using Route::is() to check if the route is the homepage - php

How can i check to see if the current route is the root page?
i've tried in a blade file typing thie following:
#if(Route::is('/')
//show something
#else
//show something else
#endif
i've also tried it with Route::currentRouteName() such as
#if(Route::currentRouteName() == '/')
//do something
#else
//do something else
#endif
and it doesn't seem to work on the root page.
is there an alternative way? or is it that the root doesn't have a route?

#if(Request::is('/'))
//do something
#else
//do something
#endif

Route::get('/', 'HomeController#index')->name('root');
#if(Route::currentRouteName() == 'root')
//do something
#else
//do something else
#endif

you can use your function to check currentRouteName, you just need to make sure you set the name for that route like I mentioned below:
Route::get('/', 'HomeController#index')->name('home');
#if(Route::currentRouteName() == 'home')
//do something
#else
//do something else
#endif

Alternatively, you can also use routeIs():
#if(request()->routeIs('home'))
// yay
#else
// nay
#endif
The helper works like the normal "is", which means that it supports multiple patterns, but matches against route names instead.

Related

On what basics we give url and name in laravel route file

You may find me stupid but i am unable to understand on what basics we give url and name in our route file
Example:
Route::get('/order/getOrders', 'OrderController#getOrders')-
>name('order.getOrders')->middleware('auth');
can anyone please tell me.
and if we take url on the basics of where our file in view folder like( order->getorder blade file)
Then what if my path is layouts.site.topbar
In view instead of pages, my file is in layouts.
EDIT:
blade file
<a href="{{ route('sync.index') }}">
#if(isset($syncs))
#foreach ($syncs as $sync)
#endforeach
{{ $sync->session_date }}
#endif
</a>
controller file
class TopbarController extends Controller
{
public function index()
{ die('o');
$syncNames = Sync::select('session_date','session_time')->where('user_id',$user_id)->get();
return view('layouts.site.topbar', array(
'syncs' =>$syncNames
));
}
public function sync_finish_session() {
die('s');
$user_id = Auth::id();
$sync_date = date('M d ',strtotime("now"));
$sync_time = date('M d, Y H:i:s',strtotime("now"));
$sync = Sync::where('user_id',$user_id)->get();
if(count( $sync) > 0) {
Sync::where('user_id',$user_id)->update(['session_date'=>$sync_date,'session_time'=>$sync_time,'user_id'=>$user_id]);
}
else {
$dates = new Sync();
$dates->session_date = $sync_date;
$dates->session_time = $sync_time;
$dates->user_id = $user_id;
$dates->save();
}
return $sync;
}
}
web file
Route::post('/sync_finish_session', 'TopbarController#sync_finish_session')->name('sync_finish_session')->middleware('auth');
Route::get('/sync/index', 'TopbarController#index')->name('sync.index')->middleware('auth');
Now whats the problem its giving nothing even i put die but its not going in controller file.
I think this is more a personal preference thing than that there are rules.
The convention I use is name(<model>.<action>)
This way i can create routes like
Route::get('/users/{id}/view', 'UserController#view')->name('users.specific.view')->middleware('auth');
You just name route as you do want. There is no strict rules how to name route. You can change name('order.getOrders') to name("anyName") and use new name in templates.
As the Laravel documentaton about rounting says:
Named routes allow the convenient generation of URLs or redirects for specific routes.
So, you can use this name to generate URLs or redirects. For example:
You could put this in your web.php file:
Route::get('/image/index', 'API\SettingsController#index')->name('image.index');
And call that route like this in your view:
Le met see that index!
Where the {{ route('image.index') }} references the name you gave to it.
You can name your route(s) anything you want. If you wanted, you could call your above route "mySuperCoolRouteName":
Route::get('/order/getOrders', 'OrderController#getOrders')-
>name('mySuperCoolRouteName')->middleware('auth');
and later in a view file you can use this name as a "shorthand" to get/print the URL of that route:
To My Cool Route
will be rendered to
To My Cool Route

how to prefix the route group and how to call sub route in blade view

How i create Routes Groups
Route::group(['namespace'=>'Admin','prefix'=>'admin-panel','as'=>'admin'],function(){
Route::get('/',[
'as'=>'dashboard',
'uses'=>'dashboardController#index',
]);
});
in the blade view i call the route like that
{{ url('/') }}
i also tried this
{{ route('admin.dashboard') }}
but i'm getting error like this
Sorry, the page you are looking for could not be found.
Route::group(['namespace'=>'Admin','prefix'=>'admin-panel','as'=>'admin.'],function(){
Route::get('/',[
'as'=>'dashboard',
'uses'=>'dashboardController#index',
]);
});
You are missing . after the 'as'=>'admin.'
Then you can simply use {{ route('admin.dashboard') }}
Try this easy method:
Route::group(['prefix'=>'admin-panel'],function()
{
Route::get('/', 'dashboardController#index');
................... //specify all your routes here
...................
}
And you can use
url('admin-panel/index')
Hope it works.

How to EXTEND a blade template only if it exists?

Basically I want to #extend a blade template only if it exists, and if not #extend a different template. There are a few stack overflow answers concerning using #if #endif blocks, but that only works if you are #including a file, not #extending. Ideally something like this, but it doesn't work:
#if(some_condition == true)
#extends('one')
#else
#extends('two')
#endif
If the only way is to use Blade directives, could you please provide an example? Thank you!
Try doing it like this:
#extends( $somecondition == true ? 'one' : 'two')
you can use view:exists
#if(View::exists('path.to.view.one'))
#extends('one')
#else
#extends('two')
#endif
You can use View::exists
#if (View::exists('one'))
#extends('one')
#else
#extends('two')
#endif
or
file_exists() and to get the path use resource_path() this,
#if (file_exists(resource_path('views/one.blade.php')))
#extends('one')
#else
#extends('two')
#endif
You can try this, in my case that's working.. See the docs in Laravel - https://laravel.com/docs/5.5/views
#if( file_exists('path to file one'))
#extends('one')
#else
#extends('two')
#endif
you can use the conditional to define the name of the view you want to load and then simply extend it, for example:
#php
$view = '';
if (some_condition == true) {
$view = 'one';
} else {
$view = 'two';
}
#endphp
...
#extends($view)
more info
https://laravel.com/docs/5.5/blade#php

How can I check auth user role in laravel?

I have role column in users table, and I want to check the value like this in the blade file :
#if ( {{Auth::user()->role }} == '1')
// do something
#endif
Is it possible ?
In blade files, you need to write plain PHP into the #if and others blade statements. So you would need to remove the {{ }}:
#if ( auth()->user()->role == 1)
// do something
#endif
I think you can extend a blade.
https://laravel.com/docs/5.3/blade#extending-blade
It's cool and convenient.
Latest version of Laravel will work with like this. You don't need to use {{}} here.
#if ( Auth::user()->role == 1)
// do something
#endif
#if(\Illuminate\Support\Facades\Auth::user()->hasRole('Admin') == 'Admin')
// do something
#endif

Laravel language handler in templates blade

How can i use something like {{#something}} and it will run a controller that checks for "something" so i can return it to translteable text?
My current blade template looks like following:
#layout("layouts.default")
#section("inner")
<h1>Velkommen til pornobiksen</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection
I mean, how can i change the "Velkommen til pornobiksen" tekst? I know i can make something like
View::make("template")->with("h1_text","Velkommen til pornobiksen");
But is there not a module/plugin to make it easier? By making like {{#h1_text}} and it will takes from my database or something?
What is the easists way to make this?
You need to use {{ $h1_text }} to put the variable into your blade template.
#layout("layouts.default")
#section("inner")
<h1>{{ $h1_text }}</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection
EDIT
I think I misunderstood you, it seems you are looking for localization
#layout("layouts.default")
#section("inner")
<h1>{{ Lang::get('messages.welcome') }}</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection
For localization , you can use helper function : trans
home.php
return [
'welcome' => 'Velkommen til pornobiksen'
];
View
#layout("layouts.default")
#section("inner")
<h1>{{trans('home.welcome')}}</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection

Categories