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

前端 未结 10 992
感情败类
感情败类 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 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

提交回复
热议问题