我创建了控制器IndexController和FontCorrector。我想在IndexController中使用FontCorrector方法。 我的FontCorrector代码:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FontCorrector extends Controller
{
public function ConvertToDefault($text) // Convert To Default
{
$search = array("ғ","ӣ","ҷ","ҳ","қ","ӯ");
$replace = array("г","и","ч","х","к","у");
$result = str_replace($search, $replace, $text);
$search = array("Ғ","Ӣ","Ҷ","Ҳ","Қ","Ӯ");
$replace = array("Г","И","Ч","Х","К","У");
$result = str_replace($search, $replace, $result);
$search = array("ѓ","ї","љ","њ","ќ","ў");
$replace = array("г","и","ч","х","к","у");
$result = str_replace($search, $replace, $result);
$search = array("Ѓ","Ї","Љ","Њ","Ќ","Ў");
$replace = array("Г","И","Ч","Х","К","У");
$result = str_replace($search, $replace, $result);
return $result;
}
public function ctd($text)
{
$text = $this->ConvertToDefault($text);
return $text;
}
?>
这里我的IndexController和我的方法调用正在调用没有成功调用的FontCorrector控制器方法:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\FontCorrector as fc;
class IndexController extends Controller
{
public function exec(Request $request)
{
if($request->input('lang-input') !== null)
{
echo fc::ctd($request->input('lang-input'));
}
return view("index");
}
}
?>
ErrorException:“不应静态调用非静态方法App \ Http \ Controllers \ FontCorrector :: ctd()”
答案 0 :(得分:3)
你可以像这样打电话
$fc = new FontCorrector;
echo $fc->ctd($request->input('lang-input');
但是有更好的方法可以为此做出特质
就像你的app/Traits/FontCorrector.php
创建文件一样
public function ConvertToDefault($text) // Convert To Default
{
...
return $result;
}
public function ctd($text)
{
$text = $this->ConvertToDefault($text);
return $text;
}
并在 IndexController
中namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Traits\FontCorrector;
class IndexController extends Controller
{
use FontCorrector;
public function exec(Request $request)
{
if($request->input('lang-input') !== null)
{
echo $this->ctd($request->input('lang-input')); // call it
}
return view("index");
}
}
答案 1 :(得分:2)
有多种方法可以解决这个问题,但我会给你最简单的最脏的方法,但这与面向对象的编程有很大关系,而不是Laravel。
在调用方法之前,您需要在代码中实例化FontCorrector。因为ctd()不是静态方法。
您可以通过编辑 IndexController 来执行此操作:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\FontCorrector as fc;
class IndexController extends Controller
{
public function exec(Request $request)
{
if($request->input('lang-input') !== null)
{
$fc_obj = new fc;
echo $fc_obj->ctd($request->input('lang-input'));
}
return view("index");
}
}
?>
这样做有很多不妥之处,但我知道你宁愿在其他任何事情之前有所作为。
答案 2 :(得分:2)
建议不要在另一个控制器中调用或实例化控制器实例,因为您可以使用自定义helper。
但是,如果你真的想这样做,那么在你的SELECT
(exchange || ':' || pair) AS id,
exchange,
c.pair,
c.high AS high,
c.low AS low,
c.open AS open,
c.closing AS close,
c.volume AS volume,
c.opened_at,
c.closed_at,
86400 AS interval
FROM (
SELECT
exchange,
pair,
FIRST_VALUE(closed_at)
OVER (
PARTITION BY exchange, pair, floor(EXTRACT(epoch FROM closed_at) / (86400))
ORDER BY closed_at
) opened_at,
LAST_VALUE(closed_at)
OVER (
PARTITION BY exchange, pair, floor(EXTRACT(epoch FROM closed_at) / (86400))
ORDER BY closed_at
) closed_at,
FIRST_VALUE(open)
OVER (
PARTITION BY exchange, pair, floor(EXTRACT(epoch FROM closed_at) / (86400))
ORDER BY closed_at
) open,
MAX(high)
OVER (
PARTITION BY exchange, pair, floor(EXTRACT(epoch FROM closed_at) / (86400))
ORDER BY closed_at
) high,
MIN(low)
OVER (
PARTITION BY exchange, pair, floor(EXTRACT(epoch FROM closed_at) / (86400))
ORDER BY closed_at
) low,
LAST_VALUE(close)
OVER (
PARTITION BY exchange, pair, floor(EXTRACT(epoch FROM closed_at) / (86400))
ORDER BY closed_at
) closing,
SUM(volume)
OVER (
PARTITION BY exchange, pair, floor(EXTRACT(epoch FROM closed_at) / (86400))
ORDER BY closed_at
) volume
FROM candles
WHERE exchange = 'gdax' AND pair = 'ethusd' AND closed_at > '2017-10-01 10:00:00' AND volume > 0
) c
WHERE EXTRACT(epoch FROM closed_at) - EXTRACT(epoch FROM opened_at) = ((86400 - (1 * 60)))
ORDER BY closed_at DESC
中,你可以使用laravel依赖注入,我认为这是更好的方法。
IndexController
答案 3 :(得分:2)
您需要初始化&#34; ConvertToDefault&#34;和&#34; ctd&#34;作为&#34;公共静态函数&#34;因为你是从另一个控制器访问它。
尝试这种方式:
public static function ConvertToDefault($text) // Convert To Default
{
$search = array("ғ","ӣ","ҷ","ҳ","қ","ӯ");
$replace = array("г","и","ч","х","к","у");
$result = str_replace($search, $replace, $text);
$search = array("Ғ","Ӣ","Ҷ","Ҳ","Қ","Ӯ");
$replace = array("Г","И","Ч","Х","К","У");
$result = str_replace($search, $replace, $result);
$search = array("ѓ","ї","љ","њ","ќ","ў");
$replace = array("г","и","ч","х","к","у");
$result = str_replace($search, $replace, $result);
$search = array("Ѓ","Ї","Љ","Њ","Ќ","Ў");
$replace = array("Г","И","Ч","Х","К","У");
$result = str_replace($search, $replace, $result);
return $result;
}
public static function ctd($text)
{
$text = TestController::ConvertToDefault($text);
return $text;
}