jeudi 10 avril 2014

laravel - tentant d'accéder aux avis de modèle lame fait corps vide en Laravel4 - Stack Overflow


I have a project which is using laravel4 and its blade view engine. Occasionally I have had the need to call controller methods via a view file to output dynamic data; incidentally this time its a call to a method that generates javascript code for the page. Regardless of whether this is the best way to go about things is a moot point atm as I am simply upgrading from L3 to L4.


My View is similar to:


@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

I have set up CaseStudy to load via the laravel facades and the class at current is simply:


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());
}
}

It would appear that echoing the response of CaseStudy::script is what is causing the blank body; however with no further error message I do not know what is going on. I assume that this is because my static CaseStudy's instance of View is conflicting with the instance being used by the blade engine. How would I go about having CaseStudy::script() returning a string form of the rendered view?


Thank you.




In your view


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

In your library


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

Note - CaseStudy should really be a "library" or "helper" or something. Not a controller - that does not really conform to MVC approach.



I have a project which is using laravel4 and its blade view engine. Occasionally I have had the need to call controller methods via a view file to output dynamic data; incidentally this time its a call to a method that generates javascript code for the page. Regardless of whether this is the best way to go about things is a moot point atm as I am simply upgrading from L3 to L4.


My View is similar to:


@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

I have set up CaseStudy to load via the laravel facades and the class at current is simply:


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());
}
}

It would appear that echoing the response of CaseStudy::script is what is causing the blank body; however with no further error message I do not know what is going on. I assume that this is because my static CaseStudy's instance of View is conflicting with the instance being used by the blade engine. How would I go about having CaseStudy::script() returning a string form of the rendered view?


Thank you.



In your view


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

In your library


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

Note - CaseStudy should really be a "library" or "helper" or something. Not a controller - that does not really conform to MVC approach.


0 commentaires:

Enregistrer un commentaire