Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel3:在内容中动态加载内容_Php_Laravel_Templating_Laravel 3_Blade - Fatal编程技术网

Php Laravel3:在内容中动态加载内容

Php Laravel3:在内容中动态加载内容,php,laravel,templating,laravel-3,blade,Php,Laravel,Templating,Laravel 3,Blade,我有一张表格。当用户提交表单并出现错误时,我会这样显示: 寄存器控制器 return View::make('theme-admin.user_add')->with('error_msg', validation->errors->first()); register.blade.php @if($error_msg !== null) <div class="alert red hideit max"> <div class="left"&

我有一张表格。当用户提交表单并出现错误时,我会这样显示:

寄存器控制器

return View::make('theme-admin.user_add')->with('error_msg', validation->errors->first());
register.blade.php

@if($error_msg !== null)
  <div class="alert red hideit max">
     <div class="left">
            <span class="red-icon"></span>
            <span class="alert-text">{{ $error_msg }}</span> <-- Error message is visible here.
     </div>
     <div class="right">
          <a class="close-red"></a>
     </div>
 </div>
@endif

//
    Actual HTML Form
//
{{ MESSAGE_CONTENT }} //This should be replaced with error.blade.php dynamically
//
    Actual HTML Form
//
<div class="alert red hideit max">
     <div class="left">
            <span class="red-icon"></span>
            <span class="alert-text">{{ $message }}</span> <-- Error message is visible here.
     </div>
     <div class="right">
          <a class="close-red"></a>
     </div>
 </div>
消息内容将通过error.blade.php包含

@if($error_msg !== null)
  <div class="alert red hideit max">
     <div class="left">
            <span class="red-icon"></span>
            <span class="alert-text">{{ $error_msg }}</span> <-- Error message is visible here.
     </div>
     <div class="right">
          <a class="close-red"></a>
     </div>
 </div>
@endif

//
    Actual HTML Form
//
{{ MESSAGE_CONTENT }} //This should be replaced with error.blade.php dynamically
//
    Actual HTML Form
//
<div class="alert red hideit max">
     <div class="left">
            <span class="red-icon"></span>
            <span class="alert-text">{{ $message }}</span> <-- Error message is visible here.
     </div>
     <div class="right">
          <a class="close-red"></a>
     </div>
 </div>
error.blade.php

@if($error_msg !== null)
  <div class="alert red hideit max">
     <div class="left">
            <span class="red-icon"></span>
            <span class="alert-text">{{ $error_msg }}</span> <-- Error message is visible here.
     </div>
     <div class="right">
          <a class="close-red"></a>
     </div>
 </div>
@endif

//
    Actual HTML Form
//
{{ MESSAGE_CONTENT }} //This should be replaced with error.blade.php dynamically
//
    Actual HTML Form
//
<div class="alert red hideit max">
     <div class="left">
            <span class="red-icon"></span>
            <span class="alert-text">{{ $message }}</span> <-- Error message is visible here.
     </div>
     <div class="right">
          <a class="close-red"></a>
     </div>
 </div>
如果表单成功,我将在messages区域加载success.blade.php,消息将以绿色背景显示

 return View::make('theme-admin.user_add')->with(message_content', (Load success.blade.php here))->with('message', 'You successfully registered');
你可能有逻辑

我该怎么做


Ps.图像示例:

一个干净的解决方案可能是有一个简单的警报对象,带有类型和消息

//in controller
$alert->type = 'error'; // or 'success'
$alert->class = 'red'; // or 'green'
$alert->msg = $validation->errors->first(); // or 'You successfully registered'
 return View::make('theme-admin.user_add')->with('alert', $alert);

//register.blade.php
@include('alert')

//Actual HTML Form

//alert.blade.php
@if(isset($alert))
  <div class="alert {{$alert->class}} hideit max">
     <div class="left">
            <span class="red-icon"></span>
            <span class="alert-text">{{ $alert->msg }}</span>
     </div>
     <div class="right">
          <a class="close-{{$alert->class}}"></a>
     </div>
 </div>
@endif
//在控制器中
$alert->type='error';//或“成功”
$alert->class='red';//或“绿色”
$alert->msg=$validation->errors->first();//或“您已成功注册”
返回视图::make('theme-admin.user_add')->带有('alert',$alert);
//register.blade.php
@包括('警报')
//实际HTML表单
//alert.blade.php
@如果(isset($alert))
{{$alert->msg}
@恩迪夫

一个干净的解决方案可能是有一个简单的警报对象,带有类型和消息

//in controller
$alert->type = 'error'; // or 'success'
$alert->class = 'red'; // or 'green'
$alert->msg = $validation->errors->first(); // or 'You successfully registered'
 return View::make('theme-admin.user_add')->with('alert', $alert);

//register.blade.php
@include('alert')

//Actual HTML Form

//alert.blade.php
@if(isset($alert))
  <div class="alert {{$alert->class}} hideit max">
     <div class="left">
            <span class="red-icon"></span>
            <span class="alert-text">{{ $alert->msg }}</span>
     </div>
     <div class="right">
          <a class="close-{{$alert->class}}"></a>
     </div>
 </div>
@endif
//在控制器中
$alert->type='error';//或“成功”
$alert->class='red';//或“绿色”
$alert->msg=$validation->errors->first();//或“您已成功注册”
返回视图::make('theme-admin.user_add')->带有('alert',$alert);
//register.blade.php
@包括('警报')
//实际HTML表单
//alert.blade.php
@如果(isset($alert))
{{$alert->msg}
@恩迪夫

您应该在为GET定义的路径中创建视图(
视图::make()
),然后在POST路径中处理表单输入:

//routes.php
Route::get('admin/adduser', array('as' => 'adduser', 'do' => function()
{
    return View::make('theme-admin.user_add');
}));

//route for handling form input
Route::post('register', array('before' => 'csrf', function()
 {
     $rules = array(
         //your vailidation rules here..
     );

     $validation = Validator::make(Input::all(), $rules);

     if ($validation->fails())
     {
         //re-populate form if invalid values, so add flashing input by with_input()
         //also pass errors by using with_errors
         return Redirect::to('adduser')->with_input()->with_errors($validation);
     }
     else {
         //use the named route we defined in Route:get above
         return Redirect:to('adduser')->with('message', 'Successfully added the new user!');
     }
 }));
不需要为显示错误和成功消息创建新视图。如果您想将成功和错误划分为各自的刀片模板,则可以在adduser.blade.php中使用以下内容:

//adduser.blade.php
@if($errors->has())
    @include('errormsg'); //use {{ $errors->first() }} or @foreach($errors->all() as $message) to print error message(s)
@else
    @include('success'); //use {{ $message }} to print success
@endif
但是,您也可以在视图中使用节,而是将成功和错误消息放在同一视图中:

//feedback.blade.php
@section('error')
    <em class="error">{{ $errors->first() }}</em>
@endsection

@section('success')
    <em class="success">{{ $message }}</em>
@endsection

//adduser.blade.php
@include('feedback');

@if($errors->has())
    @yield('error');
@else
    @yield('success');
@endif
//feedback.blade.php
@节(“错误”)
{{$errors->first()}
@端部
@第节(“成功”)
{{$message}}
@端部
//adduser.blade.php
@包括(“反馈”);
@如果($errors->has())
@收益率(“误差”);
@否则
@收益(“成功”);
@恩迪夫

希望有帮助。

您应该在为GET定义的路径中创建视图(
view::make()
),然后在发布路径中处理表单输入:

//routes.php
Route::get('admin/adduser', array('as' => 'adduser', 'do' => function()
{
    return View::make('theme-admin.user_add');
}));

//route for handling form input
Route::post('register', array('before' => 'csrf', function()
 {
     $rules = array(
         //your vailidation rules here..
     );

     $validation = Validator::make(Input::all(), $rules);

     if ($validation->fails())
     {
         //re-populate form if invalid values, so add flashing input by with_input()
         //also pass errors by using with_errors
         return Redirect::to('adduser')->with_input()->with_errors($validation);
     }
     else {
         //use the named route we defined in Route:get above
         return Redirect:to('adduser')->with('message', 'Successfully added the new user!');
     }
 }));
不需要为显示错误和成功消息创建新视图。如果您想将成功和错误划分为各自的刀片模板,则可以在adduser.blade.php中使用以下内容:

//adduser.blade.php
@if($errors->has())
    @include('errormsg'); //use {{ $errors->first() }} or @foreach($errors->all() as $message) to print error message(s)
@else
    @include('success'); //use {{ $message }} to print success
@endif
但是,您也可以在视图中使用节,而是将成功和错误消息放在同一视图中:

//feedback.blade.php
@section('error')
    <em class="error">{{ $errors->first() }}</em>
@endsection

@section('success')
    <em class="success">{{ $message }}</em>
@endsection

//adduser.blade.php
@include('feedback');

@if($errors->has())
    @yield('error');
@else
    @yield('success');
@endif
//feedback.blade.php
@节(“错误”)
{{$errors->first()}
@端部
@第节(“成功”)
{{$message}}
@端部
//adduser.blade.php
@包括(“反馈”);
@如果($errors->has())
@收益率(“误差”);
@否则
@收益(“成功”);
@恩迪夫

希望有帮助。

我自己找到了解决办法。我需要嵌套视图功能

$view = View::make('home');
$view->nest('content', 'orders', array('orders' => $orders));

有关更多信息,请参阅Laravel文档。

我自己找到了解决方案。我需要嵌套视图功能

$view = View::make('home');
$view->nest('content', 'orders', array('orders' => $orders));

有关更多信息,请参阅Laravel文档。

这不是我要寻找的确切解决方案。不过,谢谢你。这不是我想要的确切解决方案。不过,谢谢。有些情况下它不会被包括在内,所以我不能使用@include。问题是,如果我使用$error\u msg!==null,我还必须使用$success\u msg!==无效的这就是我现在正在使用的。()也许像@include($type)这样的东西会起作用,但这远远不是我要问的。我用一个完整的例子更新了我的答案,说明了如何处理这个问题。希望这能让事情更清楚。有些情况下它不会被包括在内,所以我不能使用@include。问题是,如果我使用$error\u msg!==null,我还必须使用$success\u msg!==无效的这就是我现在正在使用的。()也许像@include($type)这样的东西会起作用,但这远远不是我要问的。我用一个完整的例子更新了我的答案,说明了如何处理这个问题。希望这能让事情变得更清楚。很好,你找到了你需要的东西,尽管我仍然建议你不要创建显示错误/成功消息的视图,而是像我的示例中那样重定向。Nest只是with()的快捷方式,其中第二个参数用于生成视图实例。因此,在您已有视图实例的示例中,无法在其中嵌套新视图。看:我还没有试过使用鸟巢。如果它没有在视图中加载视图并解析子视图的变量,我将使用您的解决方案。这比您的解决方案好一步,也就是说,我的控制器上有一个完全模块化的视图控件。我不需要在每个视图(if/else if/else等)上打开并添加include,我只需要一个变量通过控制器直接监听视图。再也没有if's或elses了。如果验证失败,我只会嵌套->错误。如果它通过了,我会成功的。两者都将通过控制器进行处理。不过,我非常感谢你的回复。这很有启发性,我想每个人都有自己的偏好也许可以破解if/else,并将刀片模板作为with()或with_errors()的第二个参数-您至少可以在此处放入内联html,因此我想应该也可以包含刀片模板。ie:->带有_错误($validation,static::make('error',$data));我还是更喜欢IF /然而,这是非常清楚的,我认为那些“呈现逻辑”比你的路线更好。很好,您找到了所需的内容,尽管我仍然建议您不要创建用于显示错误/成功消息的视图,而是像我的示例中那样重定向。Nest只是with()的快捷方式,其中第二个参数用于生成视图实例。因此,在您已经拥有视图实例的示例中,您不能忽略