如何从视图中调用控制器 - Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22151822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:07:51  来源:igfitidea点击:

How to call a controller from within a view - Laravel

laravel

提问by Jimmyt1988

I have a view that lists timesheets.

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

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.

当我遍历我的时间表时,我想获得 DeliverableController/DropdownList 的响应,并将其放在我的可交付字段应在时间表上的位置。

  • 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?
  • A)有没有办法从视图中获取控制器的响应
  • B)有没有办法从控制器方法中获取控制器的响应,以便我可以将结果推送到视图?

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

Deliverables/_DropdownList View:

可交付成果/_DropdownList 视图:

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

Timesheet Controller:

时间表控制器:

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
    }    
}

Timesheet/Create

时间表/创建

@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

Notice the /* Get response from DeliverableController/DropdownList */

注意/* Get response from DeliverableController/DropdownList */

回答by Korush Mahdavieh

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

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

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

Example:

例子:

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

回答by Antonio Carlos Ribeiro

This far from ideal, but a controller is a class as any other one, so you might be able to:

这远非理想,但控制器与任何其他控制器一样是一个类,因此您可能能够:

with(new UsersController)->doWhatever();

But if you are in need of doing something like that looks like your controllers are not really following some design patterns and they might have too much resposibilities, just to enlist some:

但是,如果您需要做类似的事情,看起来您的控制器并没有真正遵循一些设计模式,并且它们可能有太多的责任,只是为了争取一些:

1 - A controller should not know much about your business logic. Controllers should receive a request send them to a model (or a repository) the the processed data and provide this data to a view or just redirect the user to another route.

1 - 控制器应该不太了解您的业务逻辑。控制器应该收到一个请求,将它们发送到模型(或存储库)处理过的数据,并将这些数据提供给视图,或者只是将用户重定向到另一个路由。

2 - A controller should not be called by any other piece of code than the router itself.

2 - 控制器不应由路由器本身以外的任何其他代码段调用。

3 - A view should not be aware of your classes and process things, they should receive data (already processed) from controllers and show them.

3 - 视图不应该知道您的类并处理事物,它们应该从控制器接收数据(已处理)并显示它们。

4 - The single responsibility principle that one should be responsible for doing just one. If your are calling a controller from your view, your view is being responsible for showing AND processing, and your controller is having the responsibility of a normal class (or a model or a repository) and also a controller.

4 - 单一职责原则,一个人只负责做一件事。如果您从视图中调用控制器,则您的视图负责显示 AND 处理,并且您的控制器负责普通类(或模型或存储库)和控制器。

And just with what you've said this list could go on...

就像你所说的那样,这个列表可以继续......

What Laravel developers would basically tell you to do:

Laravel 开发人员基本上会告诉你做什么:

1 - Create a controller that does almost nothing:

1 - 创建一个几乎什么都不做的控制器:

class WhateverController extends Controller {

    private $repository;

    public function __construct(RepositoryInterface $repository)
    {
        $this->repository = $repository;
    }

    public function whatever()
    {
        $data = $this->repository->processData(Input::all());

        return View::make('your.view')->with(compact($data));
    }

}

2 - And everything that you need to do to generate data to your view you do in your repository class. You can have other classes instantiated (or inject) inside your repository to do whatever you need to do:

2 - 以及为您在存储库类中执行的视图生成数据所需执行的所有操作。您可以在存储库中实例化(或注入)其他类来执行您需要执行的任何操作:

class Repository implements RepositoryInterface {

    public function processData($input)
    {
        $dataProcessor = new DataProcessor;

        return $dataProcessor->process($data);
    }

}

回答by yussan

i think is better for you to create Class Aliases.

我认为更适合你创造Class Aliases

open app/config/app.php

打开 app/config/app.php

add field on array aliases

在数组上添加字段 aliases

example :

例子 :

UserSession => App\Libraries\UserSession

and you can call it on your view simply

你可以简单地在你的视图中调用它

example :

例子 :

UserSession::getData()