【问题标题】:@include not working in Laravel5.4 [duplicate]@include 在 Laravel5.4 中不起作用 [重复]
【发布时间】:2017-12-31 12:40:29
【问题描述】:

我正在学习 larval 并在 laracasts.com 上关注视频,在关于布局和结构的第 10 课中,我尝试了 @include 但它不起作用。

以下是我的文件:

/views/layout.blade.php

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">

    <title>Album example for Bootstrap</title>

    <!-- Bootstrap core CSS -->

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">

    <!-- Custom styles for this template -->
    <link href=" {{ URL::asset('css/album.css') }}" rel="stylesheet" >
  </head>

  <body>

    <div class="collapse bg-inverse" id="navbarHeader">
      <div class="container">
        <div class="row">
          <div class="col-sm-8 py-4">
            <h4 class="text-white">About</h4>
            <p class="text-muted">Add some information about the album below, the author, or any other background context. Make it a few sentences long so folks can pick up some informative tidbits. Then, link them off to some social networking sites or contact information.</p>
          </div>
          <div class="col-sm-4 py-4">
            <h4 class="text-white">Contact</h4>
            <ul class="list-unstyled">
              <li><a href="#" class="text-white">Follow on Twitter</a></li>
              <li><a href="#" class="text-white">Like on Facebook</a></li>
              <li><a href="#" class="text-white">Email me</a></li>
            </ul>
          </div>
        </div>
      </div>
    </div>

    @inlcude('layouts.nav')   <!-- This is not working -->

    <section class="jumbotron text-center">
      <div class="container">
        <h1 class="jumbotron-heading">Album example</h1>
        <p class="lead text-muted">Something short and leading about the collection below—its contents, the creator, etc. Make it short and sweet, but not too short so folks don't simply skip over it entirely.</p>
        <p>
          <a href="#" class="btn btn-primary">Main call to action</a>
          <a href="#" class="btn btn-secondary">Secondary action</a>
        </p>
      </div>
    </section>

    <div class="album text-muted">
      <div class="container">

        <div class="row">
          <div class="card">
            <img data-src="holder.js/100px280/thumb" alt="Card image cap">
            <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
          </div>
          <div class="card">
            <img data-src="holder.js/100px280/thumb" alt="Card image cap">
            <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
          </div>
          <div class="card">
            <img data-src="holder.js/100px280/thumb" alt="Card image cap">
            <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
          </div>

          <div class="card">
            <img data-src="holder.js/100px280/thumb" alt="Card image cap">
            <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
          </div>
          <div class="card">
            <img data-src="holder.js/100px280/thumb" alt="Card image cap">
            <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
          </div>
          <div class="card">
            <img data-src="holder.js/100px280/thumb" alt="Card image cap">
            <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
          </div>

          <div class="card">
            <img data-src="holder.js/100px280/thumb" alt="Card image cap">
            <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
          </div>
          <div class="card">
            <img data-src="holder.js/100px280/thumb" alt="Card image cap">
            <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
          </div>
          <div class="card">
            <img data-src="holder.js/100px280/thumb" alt="Card image cap">
            <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
          </div>
        </div>

      </div>
    </div>

    <footer class="text-muted">
      <div class="container">
        <p class="float-right">
          <a href="#">Back to top</a>
        </p>
        <p>Album example is &copy; Bootstrap, but please download and customize it for yourself!</p>
        <p>New to Bootstrap? <a href="../../">Visit the homepage</a> or read our <a href="../../getting-started/">getting started guide</a>.</p>
      </div>
    </footer>

  </body>
</html>

/views/layouts/nav.blade.php

<div class="navbar navbar-inverse bg-inverse">
      <div class="container d-flex justify-content-between">
        <a href="#" class="navbar-brand">Album</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarHeader" aria-controls="navbarHeader" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
      </div>
    </div>  

/views/posts/index.blade.php

@extends('layout')

路线设置正确:

Route::get('/','PostsController@index');

【问题讨论】:

  • 试试@extends('views.layout')
  • 您的意思是在views/layout.blade.php 中写@include('layouts.nav') 而不是@inlcude('layouts.nav') ?
  • @OluwafemiSule: 是的 /views/layout.blade.php 中的 @include('layouts.nav') 不起作用
  • layouts 必须是您拥有 nav.blade.php 文件的 views 目录中的文件夹
  • 找到了包含来自与视图不同目录的文件的正确方法。发布与答案相同,谢谢。

标签: php laravel-5 laravel-5.4


【解决方案1】:

那是因为该文件不在 app/views 目录中。

当您调用@include('layouts.nav') 时,Blade 会自动在 app/views 目录中查找任何具有该名称的文件。

【讨论】:

  • 感谢您指出,是的,这就是不包括该文件的原因,经过更多挖掘后,我得到了正确的方法,与答案相同。
  • 好的,很酷,很高兴我能帮上忙
【解决方案2】:

Blade 在 /app/config/view.php 中的 paths 数组中查找文件,再添加一个资源路径可以达到以下目的:

'paths' => [
        resource_path('views'),
        resource_path('/../views/layouts')
    ], 

【讨论】:

  • 是的,这就是我在挖掘时想到的,但不确定是否有同样的问题,
  • @satyampathak:是的,上面的工作非常好。
猜你喜欢
  • 2017-09-18
  • 2018-01-20
  • 1970-01-01
  • 2013-06-07
  • 2015-07-06
  • 1970-01-01
  • 2023-04-06
  • 2014-06-11
  • 2015-12-22
相关资源
最近更新 更多