Laravel 5 - 在何处定义函数并在视图和控制器中调用它们
Laravel 5 - Where to define functions and call them in views & controllers
我有以下功能,想从视图中调用它。基本上我想将所有常用功能放在一个文件中。我不确定在哪里创建该文件以及如何在控制器和视图中调用它。
<?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;
}
}
到目前为止,我已经在 app/Http/Controllers 中创建了 CommonController.php 并将上面的函数放入其中。
然后在其他控制器中我尝试按以下方式调用它:
use App\Http\Controllers\Common;
class SongsController extends Controller {
public function index($id)
{
echo Common::BytesToMB('7012187');
}
}
但我收到错误消息:
Class 'App\Http\Controllers\Common' not found
好的,新的尝试。您错过了使用完整的 class 名称并添加静态关键字:
<?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');
}
}
另一个更面向对象的解决方案是使用来自父级的函数 class:
<?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');
}
}
我有以下功能,想从视图中调用它。基本上我想将所有常用功能放在一个文件中。我不确定在哪里创建该文件以及如何在控制器和视图中调用它。
<?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;
}
}
到目前为止,我已经在 app/Http/Controllers 中创建了 CommonController.php 并将上面的函数放入其中。
然后在其他控制器中我尝试按以下方式调用它:
use App\Http\Controllers\Common;
class SongsController extends Controller {
public function index($id)
{
echo Common::BytesToMB('7012187');
}
}
但我收到错误消息:
Class 'App\Http\Controllers\Common' not found
好的,新的尝试。您错过了使用完整的 class 名称并添加静态关键字:
<?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');
}
}
另一个更面向对象的解决方案是使用来自父级的函数 class:
<?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');
}
}