8

I am trying to call controller action within view in Laravel 5.1, but unable to do this.

Here is what I have tried so far:

<?php echo BridesController::getPhotographer($data->sender_id);?>

but it is giving Class 'BridesController' not found error.

halfer
  • 19,824
  • 17
  • 99
  • 186
Amrinder Singh
  • 5,300
  • 12
  • 46
  • 88

3 Answers3

9

To render a foreign action output inside the view that belongs to your current action, you need to call the foreign action from your current action, then store its output to a variable and pass it to your current view.

Calling actions from the same controller

If the action you want to render is inside the same controller you are using, you could call:

$result = $this->action();

return view('my.view',['my_rendered_action'=>$result]);

And then, inside your view, just:

{!! $my_rendered_action !!}

Take care of not rendering anything that comes from the user using this method, because {!! !!} tags won't scape dangerous inputs. If you do not need to have HTML inside your action response, always prefer using {{ }} instead.

Calling actions from another controller

If you need to share a method between more than one controller, the cleanest way is to create a trait or a Job that implements the logic, and then both controllers would use the trait or dispatch the same job.

Please refer to Shaddy's answer to this question for more information.

Hope it helps. ;)

Community
  • 1
  • 1
Rafael Beckel
  • 2,199
  • 5
  • 25
  • 36
  • i tried your route solution in Laravel 5.4, but the result is just the url path and not the view. For example. when i call {{ action('page.listAll') }} the result is "http://localhost.indalpe/page/listAll". How to render the view returned from the action? – Ricky Mar 30 '17 at 12:52
  • The route solution was marked as "Old Answer" because it was printing an URL. I edited the text now to remove its old version for clarity. To render the view, you need to store it in a variable before rendering. Please re-read the answer (I think it's clearer now). – Rafael Beckel Apr 06 '17 at 15:04
7

You can do this:

action('BridesController@getPhotographer',$data->sender_id);

Ofcourse if you are using blade you can do this

{{ action('BridesController@getPhotographer',$data->sender_id) }}

You can check it here http://laravel.com/docs/5.1/controllers#basic-controllers

Marcus Jason
  • 250
  • 4
  • 11
4

If you create your own facade, you can do this.

Controller:

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Facade;

class BridesController extends Facade {

   protected static function getFacadeAccessor() {
       //what you want
       return 'getPhotographer';
   }

   public static function getPhotographer() {
       return "photographer";
   }

}

//May it be a service provider.

config/app.php

 'BridesController'=>  App\Http\Controllers\BridesController::class,

And then you can use in your view.

<div class="panel-body">
    {{BridesController::getPhotographer()}}
</div>

I haven't tested it, but I think that should work.

Beller
  • 828
  • 1
  • 6
  • 19