How to call a controller from view - Laravel

I have a view that lists schedules.

in this view, each time list has a delivery field ... I have a DeliverableController that has a "DropdownList" action that calls the model and gets a list of results and pushes them to the delivery view (which just creates a drop-down list).

When I get hung up on my schedules, I would like to get a DeliverableController / DropdownList response and put it where my delivery field should be on the schedule.

  • A) there is a way to get the controller response from the view
  • B) is there a way to get the controller response from the controller method so that I can push the result to the view?

My code so far:

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

End Results / _DropdownList View:

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

Schedule 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

Pay attention to the answer / * Get from DeliverableController / DropdownList * /

+4
source share
3 answers

If you want to call the controller from the view, you can use the IOC container

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

Example:

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

This is far from ideal, but the controller is a class like any other, so you can:

with(new UsersController)->doWhatever();

But if you need to do something like this, it looks like your controllers don't actually follow some design patterns, and they may have too many features, just to enlist some:

1 - -. ( ) .

2 - , .

3 - , ( ) .

4 - , . , -, ( ), .

, , ...

Laravel :

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 - , , , . , ( ), , :

class Repository implements RepositoryInterface {

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

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

}
+1

, Class Aliases.

app/config/app.php

aliases

:

UserSession => App\Libraries\UserSession

.

:

UserSession::getData()
0

Source: https://habr.com/ru/post/1529842/


All Articles