doujia4759 2014-10-13 15:52
浏览 21
已采纳

如果变量== 0,则Laravel扩展/包含布局

Im using laravel 4.0 im tyring to display a layout only if a variable ==0 (just in case a user tries to navigate to the url instead of clicking through) (i know I can redirect instead of extending but this is undesirable for now)

I am trying to get the layout to only extend when the user navigates to the page manually, noajax is set to true if their is no ajax request being sent when it goes to the function, so if the user where to navigate to the url manually it will still display the page but extend the layout.

@if ($noajax==1)

@extends('layouts.master')

@endif

@section('content')

//controller

public function test($id,$model)
{

    if (Request::ajax())
        {
//$foreign_key and $model must be <> null
            if ($id == null || $model == null) {
                $this->render('../Errors/missing_arg', 'error');
                return;
            }

            if($model=="ArtObj")
            {
                $partable = "art_objects";
                $path='img/art-objects/';
            }

            $parid=$id;
            $noajax=0;

        $mediaimgs = Media::where('parent_id' , $id )->where('parent_table', $partable)->paginate(15);
        $response = Response::Json($mediaimgs);
        return View::make('/Admin/manageimage/manage_image',compact('parid','mediaimgs','model','path','noajax'));


        }
        else{

            if($model=="ArtObj")
            {
                $partable = "art_objects";
                $path='img/art-objects/';
            }

            $parid=$id;


        $mediaimgs = Media::where('parent_id' , $id )->where('parent_table', $partable)->paginate(15);
        $response = Response::Json($mediaimgs);
        $noajax = 1;
        return View::make('/Admin/manageimage/manage_image',compact('parid','mediaimgs','model','path','noajax'));
        }
}
  • 写回答

1条回答 默认 最新

  • dongyu1125 2014-10-13 16:53
    关注

    In this case you should use 2 views in controller.

    In controller you should use:

    if ($noajax) {
       return View::make('noajax');
    }
    else {
       return View::make('ajax');
    }
    

    In noajax view you can extend from any other view and if noajax and ajax have common code, you should put it in separate file and use @include in those both views to include common part of code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?