How to call a controller function inside a view in laravel 5

前端 未结 10 938
感情败类
感情败类 2020-11-29 05:23

In laravel 4 i just used a function

$varbl = App::make(\"ControllerName\")->FunctionName($params);

to call a controller function from a

相关标签:
10条回答
  • 2020-11-29 05:51

    For accessing a method of a Controller from a view page you need to give the full path of that controller in your blade page.

    use App\Http\Controllers\AdminAfterAuth;
    $admin_dtls = AdminAfterAuth::globAdmin();
    

    Here AdminAfterAuth is the controller class name and globAdmin is the method name.

    Now in your controller declare the method statically.

    public static function globAdmin(){
     $admin_val = AdminLogin::where('id',session('admin_id'))->get();
     return $admin_val;
     }
    
    0 讨论(0)
  • 2020-11-29 05:57

    Actually below should work as per Laravel 5:
    Usage: App::make(ControllerName)->functionName($parameters);
    Example: App:make("TestController")->getUserInfo('user_id' => 9);

    Please post the actual error you are getting!

    0 讨论(0)
  • 2020-11-29 05:58

    In view call the function like this:

    @php 
    
    use App\Http\Controllers\ControllerName; 
    $var = ControllerName::FunctionName();
    
    @endphp
    

    But if the function name in the controller is as:

    public function FunctionName(){
    
    return something;
    
    }
    

    Then an error will be shown:

    Non-static method App\Http\Controllers\ControllerName::FunctionName() should not be called statically(...)
    

    So to solve this problem "non-static" you have to change your function like this:

    public static function FunctionName(){
        
    return something;
        
    }
    

    Then you are all done.

    0 讨论(0)
  • 2020-11-29 06:03

    In laravel 5, you can do it like so

    In your view:

    <?php use App\Http\Controllers\ControllerName;
    echo ControllerName::functionName(); ?>
    

    The functionName should have the 'static' keyword e.g

    Controller function:

    public static function functionName() {
    return "Hello World!";
    }
    
    0 讨论(0)
  • 2020-11-29 06:04

    Just try this in your view :

    {{ ControllerName::Functionname($params); }}
    

    OR

    <?php echo ControllerName::Functionname($params);?>
    

    Refer this : http://laravel.io/forum/03-06-2014-what-is-the-proper-way-to-call-controllers-from-the-view?page=1

    0 讨论(0)
  • 2020-11-29 06:09

    Controllers methods are not supposed to be called from the view. Best options are to call the method from the method which is returning the view object, which contains the output which you then can echo in the view;

        public function bar() {
            $foo = $this->foo();
            return view( 'my.view', compact( 'foo' ) );
        }
    
        protected method foo()
        {
            return 'foobar';
        }
    

    second option to add a helpers file to the compose.json file

    "autoload": {
        "files": [
            "app/helpers.php"
        ]
    },
    

    dump your autoload and you can call these functions from anywhere inside your application

    0 讨论(0)
提交回复
热议问题