How To Add Title For Each Page In Laravel - laravel

In my laravel website all pages having same title meta but how to make different title for each page please tell me.
this is my code for dynamic showing in all the pages
<title>{{setting('site.title')}} | {{setting('site.description')}} #if(isset($data->title)) {{$data->title}} #endif</title>
file name is layouts/app.blade.php

Inside your app.blade.php, in the head section.
change:
<title>{{ config('app.name', 'Laravel') }}</title>
to:
<title>#yield('title')</title>
You can choose any name/title that seems fitting.
For other views.blade.php pages,(extending app.blade.php) you can add a title this way:
#section('title','My Title')
this comes after #extends('layouts.app')

You can specify different title for different views.
In you common header file make your title like this:
<title>#yield('page_title', 'Default Title')</title>
Then in your view file change the title to whatever you want it to be set in following way:
#section('page_title')
{{ "Your Title" }}
#endsection

Simply inside your app.blade.php
Put this: #yield('title')
In other pages just extend the layout and put the new title at the this section
#extends('layoutWhateverYouNamedIt')
#section('title')
Type your title here
#endsection

in your controller
public function aboutUs()
{
//page title
$title = 'Project | About Us';
return view('project.about_us', compact('title'));
}
for your view page
<title>{{ $title }}</title>

Inside your app.blade.php,
<head>
#yield('title')
... // other scripts
</head>
For other pages which user app.blade.php you can use it like after extending app.blade.php :
#section('title')
<title> Your Page Title Here </title>
#endsection`
Hope it helps. happy coding.

Related

Pass the data to errors custom page on laravel

I did create a custom 404 page on my Laravel project by creating a folder called errors and inside the folder I did create a file called 404.blade.php
However when I use a component and I try to pass the data with that component I get an error
<x-main-header-component></x-main-header-component>
</head>
<body>
<!-- Main Header -->
<x-main-navbar-component></x-main-navbar-component>
<!-- ... end User Menu Popup -->
error message here
<x-main-footer-component :message="$SocialMediaLinks"></x-main-footer-component>
</body>
</html>
the error that I'm getting is:
ErrorException
Undefined variable $SocialMediaLinks (View:
What can I do to pass the data to the component main-footer on errors/404.blade.php?
There are more than one ways you can achieve this. Since we have no access to 404Controller or anything like so, I usually come up with several ideas:
Directly import the targetted data source (model for example). This will guarantee result, but the view may look ugly and not that logical.
Caching: Redis, Memcache or anything will help. If your error views are using the same data source as your landing pages (navigation bar, header for example). The idea is that you will use Cache::remember() to store and get data whereever you want. Cache
View composer: (Docs). Cleanest way in my opinion. You just share data to all views (error views included) using global * regular expression.
Use helper: Since helpers are globally available, you can use it to store and retrieve data as you want.
For JavaScript framework component availability, just include it (the .js file) in the error views under resources/views/errors.
Example for the 3rd method:
In your AppServiceProvider#boot:
view()->composer('*', function ($view) {
$view->with([
'somethingToShare' => "Hello world",
]);
});
In your error views:
{{dd($somethingToShare)}}
Now when you open your targetted error view, it should show "Hello World"
I'm assuming you are calling $SocialMediaLinks in your app or footer.
if you are, then use an helper function. i'll add my example below
#if (isset($seo))
<title>{{ $seo->title }}</title>
<meta name="description" content="{{ $seo->meta_description }}">
<meta name="keywords" content="{{ $seo->meta_keywords }}">
#else
#php
$seo = getSEO(); //helper function
#endphp
<title>{{ $seo->title }}</title>
<meta name="description" content="{{ $seo->meta_description }}">
<meta name="keywords" content="{{ $seo->meta_keywords }}">
#endif

Remove whitespace from Laravel Blade dynamic page title

I am using a layout file for each of my views. I am dynamically setting the page title from my controller methods like this:
$metadata = [
'page_title' => 'My Page Title',
'page_description' => null,
];
return view('foo.index', compact('metadata'));
Then in my layout file, I am doing this to render the title:
#if(request()->route()->getName() == 'welcome' || request()->route()->getName() == 'home')
<title>Welcome!</title>
#else
<title>#yield('title')</title>
#endif
The logic is working great, the titles are getting rendered correctly. However there is extra space in the else that I am having trouble removing. It is rendering like this:
<title> Dynamic Page Title
</title>
Ideally, it would render like this:
<title>Dynamic Page Title</title>
I have seen a solution that others have provided but this does not seem to be resolving my issue (for some reason).
Because I am doing this in my app.blade.php file, I am not sure of another way to pass the variable up.
In each view, I am setting the title like this:
#section('title')
{{ $metadata['page_title'] ?? 'Welcome' }}
#endsection
Thank you for any suggestions!
You can pass the title as a second parameter to #section since it's the only content of the section
#section('title', $metadata['page_title'] ?? 'Welcome')
DO NOT CLOSE THE SECTION WITH #endsection
The white space is the new line and indentation of the section

How to create template in cakephp 3.x

I'm a new student in CakePHP 3 please resolve my problem.
This is my controller file:
DirectUseController.php
<?php
class DirectUseController extends AppController {
function index() {
$this->layout = 'directuse';
}
}
?>
This is my layout file:
directuse.ctp
<!DOCTYPE html>
<html>
<head>
<title>
<?= $this->fetch('title') ?>
</title>
</head>
<body>
Bootstrap | Foundation | Materilize
<br><br>
Copyright
<br><br>
</body>
</html>
This is my index file in folder of direct use
index.ctp
<section id="mainBody">
hello
</section>
and my folder structure is:
What am I missing?
Your layout should presumably include this somewhere:
echo $this->fetch('content');
If that doesn't solve your problem, you're going to have to be more specific about what the problem is.
CakePHP prioritizes convention over configuration, so try to change your Controller name to DirectusesController, change also your layout folder's name (to DirectUses) and maybe your Model too (cake bake can easily help you), I don't know your [table name in the database] but it should be plural and in lowercase (directuses) (if you don't use database that's another story)
For your template Greg Schmidt is right
Inside your layout file you have to use this:
<?= $this->Flash->render(); ?>
<?= $this->fetch('content'); ?>
I prefer to add the <html> and <body> tags inside the default.ctp or directuse.ctp layout. On this way you do not have to rebuild your html every time. This will make your code a lot cleaner.

What is the proper way of setting the page meta title in laravel

In many examples and documents I generally see that the page title is set via someController->main.blade.php->somePage.blade.php. Something like:
SomeController.php
public function someAction()
{
$title = 'Some Title';
return view('somePage', ['title'=>$title]);
}
main.blade.php
<head>
<title> #section('title') | Page #show </title>
...
somePage.blade.php
#section ('title')
{{$title}} #parent
#endsection
Wouldn't it be mode convenient to set it directly/only over the controller and blade layout file? I mean something like:
SomeController.php
public function someAction()
{
$title = 'Some Title';
return view('somePage', ['title'=>$title]);
}
main.blade.php
<head>
<title>{{ $title }}</title>
...
Wouldn't it be better to use it in that way?
I prefer not to assign the title from the controller - it's content and should be in the template from my point of view. I like to have a section in the template like
//layout file
<title> FancyApp - #yield('title')</title>
// Template
#section('title', 'Page Title')

Parameter in Route::get caused style to disappear?

I'm still new to Laravel. Anyway, I'm making a small system of viewing and creating articles to be shown in the main site. I wanted to create a page that displays an article from the database, using a parameter from the URL, like this:
http://localhost:8000/read/1
The above, for example, will display the article with 'id' value of 1 in the database.
So it works just fine, but problem is, after I got it to work (which took me some time since I'm a newbie), the whole style just disappears from the page. I tried to rewrite everything but it still didn't work. New pages that I create include the style just fine.
This is my route line:
Route::get('read/{id}', array('as' => 'read', 'uses' => 'NewsController#readArticle'));
NewsController readArticle function:
public function readArticle($id) {
$article = NewsMessage::where('id', $id) -> first();
return View::make('news.read', array('article' => $article));
}
And the file read.blade.php (located in views/news/read.blade.php)
#extends('layouts.main')
#section('content')
{{ $article -> title }}
#stop
So the whole PHP code works fine and I manage to get the title. But for some reason, the whole style disappears and this is what I see:
http://puu.sh/ctO6J/db30fbe102.png
So any idea, what have I dont wrong that caused this? The other pages work just fine with the style included.
Thank you!
The issue is that the path to the image is incorrectly specified.
You can either use the built in asset methods or something like {{ URL::to('/')/imagepathhere/filename.jpg }}
Assuming you have your styles links wrote as:
#extends('layouts.main')
#section('styles')
<link href="css/custom.css" rel="stylesheet" />
<link href="css/styles.css" rel="stylesheet" />
#endsection
#section('content')
{{ $article -> title }}
#endsection
Correct Syntax:
#extends('layouts.main')
#section('styles')
<link href="{{ asset('css/custom.css') }}" rel="stylesheet" />
<link href="{{ asset('css/styles.css') }}" rel="stylesheet" />
#endsection
#section('content')
{{ $article -> title }}
#endsection
And make sure your css folder is located at public folder when calling with asset() helper.
This issues of disappearing the whole page styles is causing by accessing the image or css file or any other resources, and the solution is to use asset() helper function.
NOTE: YOUR ROUTES AND CONTROLLER HAS NO ISSUE, YOU DON'T NEED TO TOUCH EITHER.

Resources