如何在laravel中的控制器中包含刀片视图?

时间:2017-10-03 11:02:19

标签: php laravel model-view-controller blade

我在我的控制器中有一个递归函数,它从一个数组创建一个树视图表,在这个函数中,我需要在if条件为真时包含任务视图,这个视图只是一行不同参数的表怎么能我以正确的方式访问此视图? 问题出现在crate_task_view函数中,我需要在回答之前查看任务视图请求读取代码 谢谢...代码是打击:

这是我的TaskController

    namespace App\Http\Controllers;

    use App\Task;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\DB;

class TaskController extends Controller
{

private static $menu = array(
    'items' => array(),
    'parents' => array()
);
private static $params = [] ;

private static $user_id = 'حامد_تقی_زاده';



public static function create_tree_view($parent, $menu)
{


    $html = "";
    if ( isset( self::$menu['parents'][$parent] ) )
    {

        $html .= "<ul class='tree animate'>";
        foreach ( self::$menu['parents'][$parent] as $itemId )
        {


            $task=[

                'id' => $menu['items'][$itemId]['id'],
                'p_id' => $menu['items'][$itemId]['p_id'],
                'title' => $menu['items'][$itemId]['title'],
                'visitors' => explode( ',' , $menu['items'][$itemId]['visitors'] ),
                'members' => explode( ',' , $menu['items'][$itemId]['members'] ),
                'owners'=> explode( ',' , $menu['items'][$itemId]['owners'] ),
                'start_date' => $menu['items'][$itemId]['start_date'],
                'due_date' => $menu['items'][$itemId]['due_date'],
                'finish_date' => $menu['items'][$itemId]['finish_date'],
                'status' => $menu['items'][$itemId]['status'],
                'priority' => $menu['items'][$itemId]['priority'],
                'forecast_cost' => $menu['items'][$itemId]['forecast_cost'],
                'revenue' => $menu['items'][$itemId]['revenue'],
                'reports_count' => $menu['items'][$itemId]['reports_count'],
                'files_count' => $menu['items'][$itemId]['files_count'],
                'final_cost' => $menu['items'][$itemId]['final_cost'],
                'total_time' => $menu['items'][$itemId]['total_time'],
                'final_report' => $menu['items'][$itemId]['final_report'],
                'created_at' => $menu['items'][$itemId]['created_at'],
                'created_by' => $menu['items'][$itemId]['created_by'],
            ];
            // TASKS WITHOUT CHILD
            if( !isset( self::$menu['parents'][$itemId] ) )
            {

                $html .="<li class='tr animate'>";
                if( in_array( self::$user_id , $task['members'] ) === true && in_array( self::$user_id,$task['owners'] ) === false )
                {//member

                    self::$params = [
                        'permission_level' => 1 ,
                        'parent' => false ,
                        'task' => $task ,
                    ];

                    $html .= /*task view */.'</li>';

                }
                if ( in_array( self::$user_id,$task['owners'] ) === true && in_array( self::$user_id, $task['members'] ) === false )
                {//owner
                    self::$params = [
                        'permission_level' => 2 ,
                        'parent' => false ,
                        'task' => $task ,
                    ];

                    $html .= /*task view */.'</li>';

                }
                if( in_array( self::$user_id, $task['members'] ) === true && in_array( self::$user_id,$task['owners'] ) === true)
                {//both
                    self::$params = [
                        'permission_level' => 3 ,
                        'parent' => false ,
                        'task' => $task ,
                    ];

                    $html .= /*task view */.'</li>';

                }
                if( in_array( self::$user_id,$task['visitors'] ) === true && in_array( self::$user_id, $task['members'] ) === false && in_array( self::$user_id,$task['owners'] ) === false )
                {//visitors
                    self::$params = [
                        'permission_level' => 0 ,
                        'parent' => false ,
                        'task' => $task ,
                    ];

                    $html .= /*task view */.'</li>';

                }
            }

            // TASKS WITH CHILD
            if( isset( self::$menu['parents'][$itemId] ) )
            {
                $html .= "<li class='tr animate'>";
                if( in_array( self::$user_id, $task['members'] ) === true && in_array( self::$user_id,$task['owners'] ) === false )
                {//member
                    self::$params = [
                        'permission_level' => 1 ,
                        'parent' => true ,
                        'task' => $task ,
                    ];

                    $html .= /*task view */.'</li>';
                    $html.="
                    <img onclick='(chevron".$task['id'].")' id='chevron".$task['id']."' class='chevron' src='css/png/chevron-pointing-to-the-left.png'>
                    <input class='checkbox-chevron' id='checkbox".$task['id']."' onclick='switchClopsImgSrc(".$task['id'].")'  type='checkbox' name='subFolder'/>";
                    $html .= self::create_tree_view($itemId,$menu).'</li>';

                }
                if ( in_array( self::$user_id,$task['owners'] ) === true && in_array( self::$user_id, $task['members'] ) === false )
                {//owner
                    self::$params = [
                        'permission_level' => 2 ,
                        'parent' => true ,
                        'task' => $task ,
                    ];

                    $html .= /*task view */.'</li>';
                    $html.="
                    <img onclick='(chevron".$task['id'].")' id='chevron".$task['id']."' class='chevron' src='css/png/chevron-pointing-to-the-left.png'>
                    <input class='checkbox-chevron' id='checkbox".$task['id']."' onclick='switchClopsImgSrc(".$task['id'].")'  type='checkbox' name='subFolder'/>";
                    $html .= self::create_tree_view($itemId,$menu).'</li>';

                }
                if( in_array( self::$user_id, $task['members'] ) === true && in_array( self::$user_id,$task['owners'] ) === true )
                {//both
                    self::$params = [
                        'permission_level' => 3 ,
                        'parent' => true ,
                        'task' => $task ,
                    ];
                    $html .= /*task view */.'</li>';
                    $html.="
                    <img onclick='(chevron".$task['id'].")' id='chevron".$task['id']."' class='chevron' src='css/png/chevron-pointing-to-the-left.png'>
                    <input class='checkbox-chevron' id='checkbox".$task['id']."' onclick='switchClopsImgSrc(".$task['id'].")'  type='checkbox' name='subFolder'/>";
                    $html .= self::create_tree_view($itemId,$menu).'</li>';


                }
                if( in_array( self::$user_id,$task['visitors'] ) === true && in_array( self::$user_id, $task['members'] ) === false && in_array( self::$user_id,$task['owners'] ) === false )
                {//visitors
                    self::$params = [
                        'permission_level' => 0 ,
                        'parent' => true ,
                        'task' => $task ,
                    ];
                    $html .= /*task view */.'</li>';
                    $html.="
                    <img onclick='(chevron".$task['id'].")' id='chevron".$task['id']."' class='chevron' src='css/png/chevron-pointing-to-the-left.png'>
                    <input class='checkbox-chevron' id='checkbox".$task['id']."' onclick='switchClopsImgSrc(".$task['id'].")'  type='checkbox' name='subFolder'/>";
                    $html .= self::create_tree_view($itemId,$menu).'</li>';


                }

            }
        }
        $html .= "</ul>";
    }
    return $html;
}


public static function create_array()
{
    $tasks = DB::table('tasks')->where('visitors', 'like', '%'.self::$user_id.'%')->orderBy('p_id' , 'id')->get();

    $tasks = json_decode( $tasks,true );


    for ( $i=0 ; $i < count( $tasks ) ;  $i++ )
    {
        // Create current menus item id into array
        self::$menu['items'][$tasks[$i]['id']] = $tasks[$i];

        // Creates list of all items with children
        self::$menu['parents'][$tasks[$i]['p_id']][] = $tasks[$i]['id'];
    }

    return view('home');
}

public static function test(){

    return self::create_tree_view( 0 , self::$menu );
}

}

这是我的家庭观点

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

    <link href="{{ asset('css/styles.css') }}" rel="stylesheet">
</head>
<body>

    @php
        use App\Http\Controllers\TaskController;
        echo TaskController::test();
    @endphp


</body>
</html>

这是我的任务视图

<div class="row">
    <!--
            # # # # # # # # # # # # # # # # # # # # # # # # # # #
            #                                                   #
            # task-controls contains buttons for task actions   #
            #                                                   #
            # # # # # # # # # # # # # # # # # # # # # # # # # # #
    -->
    <div class="task-controls td">

        <div class="edit">
            <div class="action">
                <i onclick='' class='fa fa-pencil toolbar-icons' aria-hidden='true'></i>
            </div>
            <div class="description" id="edit-description">
                <span id="edit-tooltip">ویرایش</span>
            </div>
        </div>

        <div class="upload">
            <div class="action">
                <i onclick='' class='fa fa-upload toolbar-icons' aria-hidden='true'></i>
            </div>
            <div class="description" id="upload-description">
                <span id="upload-tooltip">آپلود</span>
            </div>
        </div>

        <div class="files">
            <div class="action">
                <i onclick='' class='fa fa-folder-o toolbar-icons' aria-hidden='true'></i>
            </div>
            <div class="description" id="files-description">
                <span id="files-tooltip">فایل ها</span>
            </div>
        </div>

        <div class="reports">
            <div class="action">
                <i onclick='' class='fa fa-file-text-o toolbar-icons' aria-hidden='true'></i>
            </div>
            <div class="description" id="reports-description">
                <span id="reports-tooltip">گزارش</span>
            </div>
        </div>

        <div class="letters">
            <div class="action">
                <i onclick='' class='fa fa-envelope-o toolbar-icons' aria-hidden='true'></i>
            </div>
            <div class="description" id="letters-description">
                <span id="letters-tooltip">نامه</span>
            </div>
        </div>

        <div class="search">
            <div class="action">
                <i onclick='' class='fa fa-search toolbar-icons' aria-hidden='true'></i>
            </div>
            <div class="description" id="search-description">
                <span id="search-tooltip">جستجو</span>
            </div>
        </div>

        <div class="save" style="display:none">
            <div class="action">
                <i onclick='' class='fa fa-floppy-o toolbar-icons' aria-hidden='true'></i>
            </div>
            <div class="description" id="save-description">
                <span id="save-tooltip">ذخیره</span>
            </div>
        </div>

        <div class="cancel" style="display:none">
            <div class="action">
                <i onclick='' class='fa fa-times toolbar-icons' aria-hidden='true'></i>
            </div>
            <div class="description" id="cancel-description">
                <span id="cancel-tooltip">لغو</span>
            </div>
        </div>

        <div class="active" style="display:none">
            <div class="action">
                <i onclick='' class='fa fa-check toolbar-icons' aria-hidden='true'></i>
            </div>
            <div class="description" id="active-description">
                <span id="active-tooltip">فعالسازی</span>
            </div>
        </div>

        <div class="sub-task">
            <div class="action">
                <i onclick='' class='fa fa-arrow-left toolbar-icons' aria-hidden='true'></i>
            </div>
            <div class="description" id="sub-task-description">
                <span id="sub-task-tooltip">زیرگروه</span>
            </div>
        </div>

    </div>


    <!--
            # # # # # # # # # # # # # # # # # # #
            #                                   #
            # content-row contains task content #
            #                                   #
            # # # # # # # # # # # # # # # # # # #
    -->
    <div class="content-row">
        <div id="title{{$tasks['id']}}" class="content-container td">
            <div class="title one content-div">{{ $params['task']['title'] }}</div>
        </div>

        <div id="vistors{{$tasks['id']}}" class="content-container td">
            <div class="visitors two content-div">{{$tasks['visitors']}}</div>
        </div>

        <div id="members{{$tasks['id']}}" class="content-container td">
            <div class="members three content-div">{{$tasks['members']}}</div>
        </div>

        <div id="owners{{$tasks['id']}}" class="content-container td">
            <div class="owners four content-div">{{$tasks['owners']}}</div>
        </div>

        <div id="start-date{{$tasks['id']}}" class="content-container td">
            <div class="start-date five content-div">{{$tasks['start_date']}}</div>
        </div>

        <div id="due-date{{$tasks['id']}}" class="content-container td">
            <div class="due-date six content-div">{{$tasks['due_date']}}</div>
        </div>

        <div id="finish-date{{$tasks['id']}}" class="content-container td">
            <div class="finish-date seven content-div">{{$tasks['finish_date']}}</div>
        </div>

        <div id="status{{$tasks['id']}}" class="content-container td">
            <div class="status eight content-div">{{$tasks['status']}}</div>
        </div>

        <div id="forecast-cost{{$tasks['id']}}" class="content-container td">
            <div class="forecast-cost nine content-div">{{$tasks['forecast_cost']}}</div>
        </div>

        <div id="final-cost{{$tasks['id']}}" class="content-container td">
            <div class="final-cost ten content-div">{{$tasks['final_cost']}}</div>
        </div>

        <div id="revenue{{$tasks['id']}}" class="content-container td">
            <div class="revenue eleven content-div">{{$tasks['revenue']}}</div>
        </div>

        <div id="total-time{{$tasks['id']}}" class="content-container td">
            <div class="total-time twelve content-div">{{$tasks['total_time']}}</div>
        </div>

        <div id="priority{{$tasks['id']}}" class="content-container td">
            <div class="priority thirteen content-div">{{$tasks['priority']}}</div>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

替换

 $html .= /*task view */.'</li>';

使用

 $html .= view('task.view.name')->render().'</li>';