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

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

    If you have a function which is being used at multiple places you should define it in helpers file, to do so create one (may be) in app/Http/Helpers folder and name it helpers.php, mention this file in the autoload block of your composer.json in following way :

    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/"
        },
        "files": [
            "app/Http/Helpers/helpers.php"
        ]
    },
    

    run composer dump-autoload, and then you may call this function from anywhere, let it be controller view or model.

    or if you don't need to put in the helpers. You can just simply call it from it's controller. Just make it a static function. Create.

    public static function funtion_name($args) {}
    

    Call.

    \App\Http\Controllers\ControllerName::function_name($args)
    

    If you don't like the very long code, you can just make it

    ControllerName::function_name($args)
    

    but don't forget to call it from the top of the view page.

    use \App\Http\Controllers\ControllerName;
    

提交回复
热议问题