【问题标题】:Including to different headers to a same page包括到同一页面的不同标题
【发布时间】:2014-08-08 07:12:23
【问题描述】:

我有这个文件名为file-not-found.php 并具有不同的标题.. 我想要做的是根据路由名称或 url 段更改标题

CodeIgniter 我可以做到这一点

function blah(){
    include('layout.registered.header')
    include('layout.file-not-found')
}

function unregistered(){
    include('layout.unregistered.header')
    include('layout.file-not-found')
}

现在.. 我怎样才能在 Laravel Blade 模板中获得这个而不使用控制器.. 我只使用路线和视图

我尝试了下面的方法,它在IF中调用了extends,还在ELSE处调用了extends,因此它同时显示了2个布局

file-not-found.blade.php

@if (Route::currentRouteName() == 'file-not-found')
    @extends('layouts.unregistered')

@else
    @extends('layouts.registered')

@endif

路线

Route::get('/file-not-found', array('as' => 'file-not-found', function()
{
    return View::make('pages.new.file-not-found');
}));


Route::get('registered/file-not-found', array('as' => 'registered', function()
{
    return View::make('pages.new.file-not-found');
}));

【问题讨论】:

    标签: php laravel laravel-4 blade


    【解决方案1】:

    你可以试试这个:

    @extends(Route::currentRouteName() == 'file-not-found' ? 'layouts.unregistered' : 'layouts.registered')
    

    【讨论】:

      【解决方案2】:

      您使用的是哪个版本的 Laravel?我相信从 4.1 版开始,您需要使用 Route::current()->getName() 而不是 Route::currentRouteName() 来访问当前路由。

      https://github.com/laravel/framework/issues/2919。看来主要文档还没有更新

      【讨论】:

      • 它 (Route::currentRouteName()) 工作 4.1.9,经过测试。
      • 嗯...奇怪,从 4.0 到 4.1 的 upgrade 文档说“现在通过 Route::current() 而不是 Route::getCurrentRoute() 访问当前路线”。猜猜两者仍然受支持。我喜欢你的技巧,不过对于单个扩展...我会使用那个:)
      • 有一个current() 方法可用。
      猜你喜欢
      • 2022-01-09
      • 2013-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-19
      • 1970-01-01
      • 2015-10-22
      • 1970-01-01
      相关资源
      最近更新 更多