Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5-在何处定义函数并在视图中调用它们&;控制器_Php_Helper_Laravel 5 - Fatal编程技术网

Php Laravel 5-在何处定义函数并在视图中调用它们&;控制器

Php Laravel 5-在何处定义函数并在视图中调用它们&;控制器,php,helper,laravel-5,Php,Helper,Laravel 5,我有以下函数,想从视图中调用它。基本上我想把所有常用函数放在一个文件中。我不知道在哪里创建该文件,以及如何在控制器和视图中调用它 <?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use DB; class CommonController extends Controller { public function BytesToMB($bytes=0) { if(empty($

我有以下函数,想从视图中调用它。基本上我想把所有常用函数放在一个文件中。我不知道在哪里创建该文件,以及如何在控制器和视图中调用它

<?php namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use DB;

class CommonController extends Controller {

public function BytesToMB($bytes=0)
{
    if(empty($bytes))
    return 0;

    $kb = ceil($bytes/1024);

    $mb = ceil($kb/1024);

    return $mb;
}

}
但我得到了一个错误:

 Class 'App\Http\Controllers\Common' not found

好的,新的尝试。您没有使用完整的类名并添加static关键字:

<?php 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use DB;

class CommonController extends Controller {

    public static function BytesToMB($bytes=0)
    {
        if(empty($bytes))
        return 0;

        $kb = ceil($bytes/1024);

        $mb = ceil($kb/1024);

        return $mb;
    }

}

我添加了公共静态,但不工作。我认为这是第二件事,因为错误表明找不到类。我编辑了我的答案,因为我太晚才意识到课堂本身的缺失:pnot不起作用,我也编辑了问题。我只尝试了Common而不是CommonController,但它是一样的。。只需编辑对//的回答即可,如果您在同一命名空间中,则无需定义,请使用App\Http\Controllers\CommonController;还请告诉我如何在视图中访问此功能!
<?php 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use DB;

class CommonController extends Controller {

    public static function BytesToMB($bytes=0)
    {
        if(empty($bytes))
        return 0;

        $kb = ceil($bytes/1024);

        $mb = ceil($kb/1024);

        return $mb;
    }

}
<?php
namespace App\Http\Controllers;

// You do not need to define this, if you are in the same namespace
use App\Http\Controllers\CommonController;

class SongsController extends Controller {
    public function index($id)
    {
       echo CommonController::BytesToMB('7012187');
    }
}
<?php 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use DB;

class CommonController extends Controller {

    protected function BytesToMB($bytes=0)
    {
        if(empty($bytes))
        return 0;

        $kb = ceil($bytes/1024);

        $mb = ceil($kb/1024);

        return $mb;
    }

}
<?php
namespace App\Http\Controllers;

// You do not need to define this, if you are in the same namespace
use App\Http\Controllers\CommonController;

class SongsController extends CommonController {

    public function index($id)
    {
       echo $this->bytesToMB('7012187');
    }
}