Created
June 28, 2021 23:05
-
-
Save m5lil/87c895fc5c0fc38cbcbe8251a3d9b075 to your computer and use it in GitHub Desktop.
[View composers] Set Meta Titles with View Composers #laravel #example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use App\Http\ViewComposers\MetaViewComposer; | |
use Illuminate\Support\Facades\View; | |
use Illuminate\Support\ServiceProvider; | |
class AppServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Register any application services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
// | |
View::composer('admin.layouts.app', MetaViewComposer::class); | |
} | |
/** | |
* Bootstrap any application services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
// | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\ViewComposers; | |
use Illuminate\Support\Facades\Request; | |
use Illuminate\View\View; | |
class MetaViewComposer | |
{ | |
public function compose(View $view) | |
{ | |
$view->with('metaTitle', ucfirst(Request::segment(count(Request::segments())))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment