Reputation: 1813
I am working on a Laravel
project implementing React
for the front end.
I've set up a couple of routes and have created two blade files for both the dashboard and a clients list page.
However, after consideration I feel that maybe a single blade file with a header and footer and just a #content
div in the middle would be more efficient.
I cannot see how to specific which blade file for the route. At first, I thought I was specifying it using the name()
function on the Route but I am wrong. Here are my routes in my web.php
file:
Auth::routes();
Route::get('/', 'HomeController@index')->name('home');
Route::get('/clients', 'ClientController@index')->name('clients');
And I created two blade files:
home.blade.php
clients.blade.php
Which basically contain:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Dashboard</div>
<div class="card-body">
@if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
@endif
<div id="content"></div>
</div>
</div>
</div>
</div>
</div>
@endsection
Except in the clients template I have the title 'Clients' not 'Dashboard'.
This is the app.blade.php
which each template extends:
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSRF Token -->
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>{{ config('app.name', 'Reserve Shark') }}</title>
<!-- Scripts -->
<script src="{{ mix('js/app.js') }}" defer></script>
<!-- Fonts -->
<link rel="dns-prefetch" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet" type="text/css">
<!-- Styles -->
<link href="{{ mix('css/app.css') }}" rel="stylesheet">
</head>
<body>
<div id="app">
<nav class="navbar navbar-expand-md navbar-light navbar-laravel">
<div class="container">
<a class="navbar-brand" href="{{ url('/') }}">
{{ config('app.name', 'Reserve Shark') }}
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="{{ __('Toggle navigation') }}">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<!-- Left Side Of Navbar -->
<ul class="navbar-nav mr-auto">
</ul>
<!-- Right Side Of Navbar -->
<ul class="navbar-nav ml-auto">
<!-- Authentication Links -->
@guest
<li class="nav-item">
<a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
</li>
@else
<li class="nav-item dropdown">
<a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="{{ route('logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
{{ __('Logout') }}
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
@csrf
</form>
</div>
</li>
@endguest
</ul>
</div>
</div>
</nav>
<main class="py-4">
@yield('content')
</main>
</div>
</body>
</html>
However, when I changed the client route's name to 'home' (thinking it would use the home blade file) it didn't change. So, for the purpose of testing, I changed it to 'blah' which didn't change anything. I even changed the route itself to client-page and the same in my app.js
file:
render((
<Router history={browserHistory}>
<Route path="/" component={Example} />
<Route path="/client-page" component={Client} />
</Router>
), document.getElementById('content')
);
Yet there is no change.
When I changed my clients.blade.php file to clients2.blade.php
I got an error stating that clients.blade.php
didn't exist.
So my question is: where on earth is Laravel stipulating that this page uses THAT blade file?
Is it possible to somehow change it so every route uses the same file and the only difference is the data that's injected into the #content
div?
Upvotes: 0
Views: 318
Reputation: 5633
When you use the ->name('home')
attach to your route, you just specifie the name of the route by default Laravel will name your route as home.route
, and this is just to help you remember the name of your route when you whan to return same URL instead of writing the URL manualy you can use the route()
helper
To specifie the blade template which you want to render you must put in you index method of you HomeController
class HomeController extends Controller
{
public function index()
{
/* here goes all other code */
return View::make('home'); // Laravel will render your home.blade.php file
}
}
class ClientController extends Controller
{
public function index()
{
/* here goes all other code */
return View::make('clients'); // Laravel will render your clients.blade.php file
}
}
don't forget to use the Illuminate\Supports\Facades\View
Upvotes: 1
Reputation: 4202
In your controller (e.g. ClientController), within the index
method, there should either be a view(...)
helper method or a View::make(...)
static method.
Change the value to change the blade template used.
Upvotes: 1