【问题标题】:Attempting to access View from blade template causes blank body in Laravel4尝试从刀片模板访问视图导致 Laravel4 中的空白正文
【发布时间】:2013-08-02 07:44:53
【问题描述】:

我有一个使用 laravel4 及其刀片视图引擎的项目。有时我需要通过视图文件调用控制器方法来输出动态数据;顺便说一句,这一次它调用了一个为页面生成 javascript 代码的方法。不管这是否是最好的处理方式都是有争议的,因为我只是从 L3 升级到 L4。

我的视图类似于:

@extends('en/frontend/layouts/default')

{{-- Page title --}}
@section('title')
    Page Title
    @parent
@stop

{{-- Page content --}}
@section('pageContent')
    ...
@stop

{{-- Scripts --}}
@section('scripts')
    <?php echo CaseStudy::script(); ?>
@stop

我已经将 CaseStudy 设置为通过 laravel 外观加载,当前的类很简单:

class CaseStudy
{

    public function display()
    {

    }

    /**
     * Returns the javascript needed to produce the case study
     * interactivity
     *
     * @return \Illuminate\View\View|void
     */
    public function script()
    {
        $language = \Config::get('app.locale');

        $scriptElementView = $language . '.frontend.elements.casestudy_script';

        if ( ! \View::exists($scriptElementView))
        {
            $scriptElementView = "Training::" . $scriptElementView;
        }

        return \View::make($scriptElementView, array());
    }
}

似乎是响应 CaseStudy::script 的响应是导致空白正文的原因;但是没有进一步的错误消息,我不知道发生了什么。我认为这是因为我的静态 CaseStudy 的 View 实例与刀片引擎正在使用的实例冲突。我将如何让 CaseStudy::script() 返回呈现视图的字符串形式?

谢谢。

【问题讨论】:

    标签: laravel laravel-4 blade


    【解决方案1】:

    在你看来

    {{-- Scripts --}}
    @section('scripts')
        {{ CaseStudy::script() }}
    @stop
    

    在你的图书馆里

    class CaseStudy
    {
        public function script()
        {
             $string = "your script here";
             return $string;
        }
    }
    

    注意 - CaseStudy 应该是真正的“图书馆”或“助手”或其他东西。不是控制器 - 并不真正符合 MVC 方法。

    【讨论】:

    • 感谢您的回复,是的,此代码位于作为包的一部分的帮助器类中。我发现这个错误与 View 无关,更多的是与 laravel3 方法 URL::to_route() 潜入我的 laravel4 视图代码有关。一旦我修复它一切运行良好 - 刀片破坏调试只是一种耻辱,否则我会更快地抓住它。
    猜你喜欢
    • 2013-06-03
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 2013-05-08
    • 2017-08-07
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多