如何从视图中调用控制器-Laravel [英] How to call a controller from within a view - Laravel

查看:50
本文介绍了如何从视图中调用控制器-Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列出时间表的视图.

I have a view that lists timesheets.

在该视图上,每个时间表都有一个可交付字段...我有一个DeliverableController,它具有一个操作"DropdownList",该操作会调用模型并获取可交付成果的列表,并将其推入可交付成果视图(这只会创建一个下拉框).

on that view, each timesheet has a deliverable field... I have a DeliverableController that has an action "DropdownList" that calls upon the model and gets a list of deliverables and pushes them to a deliverable view (which just creates a dropdown box).

即时通讯在我的时间表中循环时,我想获取DeliverableController/DropdownList的响应,并将其放在时间表中我的可交付字段应位于的位置.

When im looping through my timesheets I would like to get the response of the DeliverableController/DropdownList and put it where my deliverable field should be on the timesheet.

  • A)是否可以从视图中获取控制器的响应
  • B)是否可以从控制器方法中获取控制器的响应,以便将结果推送到视图?
  • A) is there a way of getting the response of a controller from within a view
  • B) is there a way of getting the response of a controller from within a controller method, so that I can push the result to the view?

到目前为止,我的代码是:

My code so far is:

DeliverableController:

class DeliverableController extends BaseController {   
    private $deliverableRepository;

    function __Construct( IDeliverableRepository $deliverableRepo )
    {
        $this->deliverableRepository = $deliverableRepo;
    }

    ...

    public /*  */ function DropdownList()
    {        
        $deliverables = $this->deliverableRepository->Deliverables(); 
        return View::make( 'Deliverable/_DropdownList', array( "Model" => $deliverables ) );
    }
}

可交付成果/_DropdownList视图:

<?php
    foreach( $Model as $item )
    {
?>
    <select name="">
        <option value = "<?php echo $item->ID; ?>"><?php echo $item->Title; ?></option>
    </select>    
<?php
    }
?>

时间表控制器:

class TimesheetController extends BaseController {      
    private $timesheetRepository;

    function __Construct( ITimesheetRepository $timesheetRepo )
    {
        $this->timesheetRepository = $timesheetRepo;
    }

    ...

    // [HttpGET]
    public /*  */ function GetCreate()
    {       
        return View::make( 'Timesheet/Create' );
    }   

    // [HttpPOST]
    public /*  */ function PostCreate()
    { 
        // To do
    }    
}

时间表/创建

@extends( 'layout' )

@section( 'Styles' )
    <link href="<?php echo Request::root(); ?>/Styles/Timesheet.css" rel="stylesheet">
@stop

@section( 'Title' )
    Create timesheets
@stop

@section( 'Content' )

<form role="form">
    <table id="TimesheetTable" class="table">
        <thead>
            <tr>
                <th>Project/Deliverable</th>
                ...                
            </tr>
        </thead>
        <tfoot>
            <tr>
                <td></td>
                ...
            </tr>
        </tfoot>
        <tbody>
            <?php
                for( $a = 0; $a < 18; $a++ )
                {
            ?>
                    <tr id='row<?php echo $a; ?>'>
                        <td><?php /* Get response from DeliverableController/DropdownList */ ?></td>...                    
                    </tr>
            <?php
                }
            ?>
        </tbody>
    </table>
@stop

@section( 'Scripts' )
    <script src="<?php echo Request::root(); ?>/Scripts/Timesheet.js"></script>
@stop

请注意/*从DeliverableController/DropdownList获取响应*/

推荐答案

如果您想从视图中调用控制器,则可以使用IOC容器

If you would like to call a controller from a view you can use the IOC container

    App::make(//ControllerName)->//methodName(//parameters);

示例:

    App::make("UserController")->displayUsers(array('page_id' => 55));

这篇关于如何从视图中调用控制器-Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆