Laravel 5 Get View Name

Laravel 5 get view name

Update your AppServiceProvider by adding a view composer to the boot method and using '*' to share it with all views:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
view()->composer('*', function($view){
$view_name = str_replace('.', '-', $view->getName());
view()->share('view_name', $view_name);
});

}

/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}

{{$view_name}} will be made available to your blade templates.

How to retrieve the view name in Laravel 5?

You can use Laravel View Composers

View composers are callbacks or class methods that are called when a
view is rendered. If you have data that you want to be bound to a view
each time that view is rendered, a view composer can help you organize
that logic into a single location.

In the file app/Providers/AppServiceProvider.php, edit the public function boot() method

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(Application $app)
{
view()->composer('*', function($view) {
view()->share('view_name', $view->getName());
});
}

After this, in any blade template you will now have access to a variable

{{ $view_name }}

how to get current view name (pagename) in laravel 5.7

You can use Request::segment() to get any segment of url() like:

{{ Request::segment(1) }}

Reference

How can I get the current view name inside a master layour in Laravel 4?

Inside your filters file place:

View::composer('*', function($view){

View::share('view_name', $view->getName());

});

Then in your master blade layout you can access it as $view_name.

Get Laravel 5 controller name in view

If your layout is a Blade template, you could create a view composer that injects those variables into your layout. In app/Providers/AppServiceProvider.php add something like this:

public function boot()
{
app('view')->composer('layouts.master', function ($view) {
$action = app('request')->route()->getAction();

$controller = class_basename($action['controller']);

list($controller, $action) = explode('@', $controller);

$view->with(compact('controller', 'action'));
});
}

You will then have two variables available in your layout template: $controller and $action.

Get Controller and Action name in your view template?

A few options.

Pass variables to the view from your controller.
Set a global view variable
use view composer.
You need to choose one of these depending on your use case.

Route::getCurrentRoute()->getAction();
Route::currentRouteAction();
Route::currentRouteName();

Laravel 5.4 View [name] not found

I was having this problem only when running the application on homestead server and i was able to solve this after i ran the

php artisan config:clear

command

Get the current view in a Laravel controller

I managed to create a new way to call the "create PDF" function without having to add code to the PrintController.

PrintController.php

<?php
class PrintController extends \BaseController{

public function show( $id )
{
if( isset($id) )
{
/** remove last 2 character */
$class_name = substr( $id, 0, -2 );

/** Stay only last character */
$id = substr( $id, -1 );

if( isset($class_name) )
{
$with_method = 'with'.ucfirst( $class_name ); // withClient
$find_method = ucfirst( $class_name ); // Client
$html = View::make( $class_name .'s.show' )->$with_method( $find_method::find($id) );
}
else
{
$html = 'Data Not Found!';
}

$pdf = App::make( 'dompdf' );
$pdf->loadHTML( $html );
return $pdf->stream();
}
}
}

View (Only the client and $client to remplace)

<a href="{{ URL::to('imprimer/'.'client='. $client->id) }}" target="_blank">
<button class="btn btn-primary pull-right mgright10">PDF2</button>
</a>


Related Topics



Leave a reply



Submit