【问题标题】:Laravel blade template not rendering with slugLaravel 刀片模板不使用 slug 渲染
【发布时间】:2016-02-27 12:58:13
【问题描述】:

嗨,也许我在这里遗漏了一些东西,但是如果我使用诸如

之类的路线

/admin-user-management

然后刀片模板正确渲染。但是使用确切的代码,复制/粘贴到带有路线的另一个视图中

/admin/{id}

然后视图不渲染模板。

两个视图在各个方面都是相同的,一个使用模板正确渲染视图,而另一个带有 slug 的视图不能正确渲染模板,它会随机吐出元素但它不能正确渲染实际布局.

为什么它们会有所不同?这也不是我第一次遇到这种情况,我只是不知道如果 URL 中是否有 slug 会有什么区别。

再一次,两个文件是相同的。

一个是useraccounts函数,另一个是控制器上的show函数。

public function show($id)
{
    $user=User::find($id);
    return view('admin.show',compact('user'));
}

public function useraccounts()
{
    $users = User::all();
    return view('admin.useraccounts', compact('users'));
}

显示用户详情的按钮如下

<a href="{{route('admin.show',$user)}}" class="btn btn-primary">View all information</a>

请问有人对我遇到的这个问题有任何想法吗?


admin.show 视图

@extends('admin')

@section('main')
    <aside class="right-side">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                Dashboard
                <small>Control panel</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="/admin"><i class="fa fa-dashboard"></i> Home</a></li>
                <li class="active">Dashboard</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content">

            <!-- Small boxes (Stat box) -->
            <div class="row">
                <div class="col-lg-3 col-xs-6">
                    <!-- small box -->
                    <div class="small-box bg-green">
                        <div class="inner">
                            <h3>
                                {{--53<sup style="font-size: 20px">%</sup>--}}
                            </h3>
                            <p>
                                Statistics
                            </p>
                        </div>
                        <div class="icon">
                            <i class="ion ion-stats-bars"></i>
                        </div>
                        <a href="#" class="small-box-footer">
                            View Information <i class="fa fa-arrow-circle-right"></i>
                        </a>
                    </div>
                </div><!-- ./col -->
                <div class="col-lg-3 col-xs-6">
                    <!-- small box -->
                    <div class="small-box bg-yellow">
                        <div class="inner">
                            <h3>
                                {{--53--}}
                            </h3>
                            <p>
                                User Management
                            </p>
                        </div>
                        <div class="icon">
                            <i class="ion ion-person-add"></i>
                        </div>
                        <a href="/admin-user-management" class="small-box-footer">
                            Edit Details <i class="fa fa-arrow-circle-right"></i>
                        </a>
                    </div>
                </div><!-- ./col -->
                <div class="col-lg-3 col-xs-6">
                    <!-- small box -->
                    <div class="small-box bg-red">
                        <div class="inner">
                            <h3>
                                {{--65--}}
                            </h3>
                            <p>
                                Detailed Site Information
                            </p>
                        </div>
                        <div class="icon">
                            <i class="ion ion-pie-graph"></i>
                        </div>
                        <a href="#" class="small-box-footer">
                            View Data <i class="fa fa-arrow-circle-right"></i>
                        </a>
                    </div>
                </div><!-- ./col -->
            </div><!-- /.row -->

            <!-- top row -->
            <div class="row">
                <div class="col-xs-12 connectedSortable">

                </div><!-- /.col -->
            </div>

        </section><!-- /.content -->
        <label>{{$user->name}}</label>
    </aside><!-- /.right-side -->
@endsection

然后是正在使用的 admin.admin-user-management 视图

@extends('admin')

@section('main')
    <aside class="right-side">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                Dashboard
                <small>Control panel</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="/admin"><i class="fa fa-dashboard"></i> Home</a></li>
                <li class="active">Dashboard</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content">

            <!-- Small boxes (Stat box) -->
            <div class="row">
                <div class="col-lg-3 col-xs-6">
                    <!-- small box -->
                    <div class="small-box bg-green">
                        <div class="inner">
                            <h3>
                                {{--53<sup style="font-size: 20px">%</sup>--}}
                            </h3>
                            <p>
                                Statistics
                            </p>
                        </div>
                        <div class="icon">
                            <i class="ion ion-stats-bars"></i>
                        </div>
                        <a href="#" class="small-box-footer">
                            View Information <i class="fa fa-arrow-circle-right"></i>
                        </a>
                    </div>
                </div><!-- ./col -->
                <div class="col-lg-3 col-xs-6">
                    <!-- small box -->
                    <div class="small-box bg-yellow">
                        <div class="inner">
                            <h3>
                                {{--53--}}
                            </h3>
                            <p>
                                User Management
                            </p>
                        </div>
                        <div class="icon">
                            <i class="ion ion-person-add"></i>
                        </div>
                        <a href="#" class="small-box-footer">
                            Edit Details <i class="fa fa-arrow-circle-right"></i>
                        </a>
                    </div>
                </div><!-- ./col -->
                <div class="col-lg-3 col-xs-6">
                    <!-- small box -->
                    <div class="small-box bg-red">
                        <div class="inner">
                            <h3>
                                {{--65--}}
                            </h3>
                            <p>
                                Detailed Site Information
                            </p>
                        </div>
                        <div class="icon">
                            <i class="ion ion-pie-graph"></i>
                        </div>
                        <a href="#" class="small-box-footer">
                            View Data <i class="fa fa-arrow-circle-right"></i>
                        </a>
                    </div>
                </div><!-- ./col -->
            </div><!-- /.row -->

            <!-- top row -->
            <div class="row">
                <div class="col-xs-12 connectedSortable">

                </div><!-- /.col -->
            </div>

        </section><!-- /.content -->
        <div class="container-fluid">
            <div class="form-box">

                @if (session('message'))
                    <div class="alert alert-info" style="text-align: center; width: 200px; ">
                        {{ session('message') }}
                    </div>
                @endif

                <div class="container">

                    {{-- @if(Auth::user()) --}}
                    <form class="form-group">
                        @if($users != null)
                            @foreach($users as $user)
                                @if($user->admin != 'true')
                                    {!! Form::label($user->name) !!}
                                    <div class="container">
                                        <br>
                                        <a href="{{route('admin.show', ['id' => $user->id])}}" class="btn btn-primary">View all information</a>
                                    </div>
                                    </br>
                                    <div class="container">
                                        {{-- <a href="{{route('house.edit',$house)}}" class="btn btn-primary">Update house details</a> --}}
                                    </div>
                                    <br>

                                    <div class="container">
                                        {{-- {!! Form::open(['method' => 'DELETE', 'route'=>['house.destroy', $house->id]]) !!} --}}
                                        {{-- {!! Form::submit('Delete House', ['class' => 'btn btn-danger']) !!} --}}
                                        {{-- {!! Form::close() !!} --}}
                                    </div>
                                @endif
                            @endforeach
                        @else
                            {{-- <li><a href={{url('/house/create')}}>Add new house</a></li> --}}
                        @endif
                    </form>
                    {{-- @endif --}}
                </div>
            </div>
        </div>
    </aside><!-- /.right-side -->
@endsection

routes.php 文件

//Admin Routes

Route::get('/admin-user-management', 'AdminController@useraccounts');
Route::resource('admin', 'AdminController');

标题

<head>
    <meta charset="UTF-8">
    <title>Admin | Dashboard</title>
    <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
    <!-- bootstrap 3.0.2 -->
    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <!-- font Awesome -->
    <link href="bootstrap/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
    <!-- Ionicons -->
    <link href="bootstrap/css/ionicons.min.css" rel="stylesheet" type="text/css" />
    <!-- Morris chart -->
    <link href="bootstrap/css/morris/morris.css" rel="stylesheet" type="text/css" />
    <!-- jvectormap -->
    <link href="bootstrap/css/jvectormap/jquery-jvectormap-1.2.2.css" rel="stylesheet" type="text/css" />
    <!-- fullCalendar -->
    <link href="bootstrap/css/fullcalendar/fullcalendar.css" rel="stylesheet" type="text/css" />
    <!-- Daterange picker -->
    <link href="bootstrap/css/daterangepicker/daterangepicker-bs3.css" rel="stylesheet" type="text/css" />
    <!-- bootstrap wysihtml5 - text editor -->
    <link href="bootstrap/css/bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css" rel="stylesheet" type="text/css" />
    <!-- Theme style -->
    <link href="bootstrap/css/AdminLTE.css" rel="stylesheet" type="text/css" />

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
    <![endif]-->
</head>

【问题讨论】:

  • 您能分享包含 css 样式表的标题部分吗
  • 我添加了标题的信息,但是正如我所提到的,即使代码重复,也只有一个正确呈现而另一个没有,所以我认为标题信息在某种程度上是无关紧要的,但如果它确实有帮助,然后我添加了它:)
  • 我猜,css文件放在public/admin/bootstrap?
  • 是的,这是正确的 :) 这似乎是路径中的问题。
  • 很高兴为您提供帮助,如果您愿意,我可以添加答案,以造福社区。​​span>

标签: templates laravel routes blade slug


【解决方案1】:

您的资产路径是相对的,它们应该是绝对的:

<link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />

注意bootstrap/css/bootstrap.min.css 之前的额外/。如果/ 丢失并且您的URL 是/admin/1,则相对路径是/admin,它会尝试在/admin/bootstrap/css/bootstrap.min.css 中查找引导CSS 文件,如果不存在的话。


这里最好的解决方案是在生成任何资产 URL 时使用 asset 辅助函数,因为它会生成一个包含协议和域的完整 URL,这样可以避免将来出现任何此类问题,如下所示:

<link href="{{ asset('bootstrap/css/bootstrap.min.css') }}" rel="stylesheet" type="text/css" />

asset 函数会生成这样的 URL:

http://example.com/bootstrap/css/bootstrap.min.css

这意味着该位置将始终正确。当然,在从服务器加载所有资产(包括 JavaScript 文件)时,您应该使用该函数,但不能在从 maxcdn.com 加载的 html5shiv 等外部资源上使用该函数。

【讨论】:

  • 嗨,如果我使用数据转储,似乎任何一种方式都会吐出正确的信息,所以这不仅仅是实现与使用 查看所有信息
  • 也许我误解了你的问题,你是说admin.show 视图渲染不正确?如果有,请附上查看代码。
  • 我在检索实际数据本身时没有任何问题,但是当我尝试渲染视图时,它只能在没有 slug 的情况下工作。添加 slug 后,模板根本不会呈现。
  • slug 你指的是{id}参数?所以它适用于访问/admin 但不适用于/admin/1 ,其中1 将是用户ID。
  • 是的,这是正确的,这是令人沮丧的问题。如果没有 slug,它会正确扩展模板,当我添加 slug 时,它会拒绝正确渲染。
猜你喜欢
  • 2016-04-04
  • 2018-10-03
  • 2016-03-17
  • 2016-07-13
  • 2018-08-15
  • 1970-01-01
  • 1970-01-01
  • 2017-01-02
  • 2013-04-29
相关资源
最近更新 更多