使用来自 Blade 的辅助方法返回 'Cannot redeclare Class' 错误
Using helper methods from Blade returning 'Cannot redeclare Class' error
我正在尝试从我的 blade 视图调用 App\Http\Timeslot::isOpen()
,但我不明白如何在不将 isOpen() 声明为静态的情况下调用它。我不能将它声明为静态的,因为我需要从构造中使用 $this->hours 。如果我不声明它是静态的 laravel returns Cannot redclare Class error
有人可以建议我应该如何编写它以便我仍然可以访问 $this->hours 变量吗?
Blade 模板:
@if(App\Http\Timeslot::isOpen())
We're open
@else
We're closed
@endif
时间段Class
<?php namespace App\Http;
use App\OpeningHour;
use Carbon\Carbon;
class Timeslot
{
protected $hours;
public function __construct()
{
$this->hours = OpeningHour::all();
}
public static function isOpen()
{
// get current date
Carbon::now()->format('w');
$open_window = $this->hours->get(Carbon::now()->format('w'));
// is it over current days' opening hours?
if(Carbon::now()->toTimeString() > $open_window->opening_time)
return true;
else
return false;
}
}
您也可以将 $hours 声明为静态的,并在 isOpen() 中调用方法 retrieve/cache 它们。
类似于:
<?php namespace App\Http;
use App\OpeningHour;
use Carbon\Carbon;
class Timeslot
{
static protected $hours = null;
public static function getOpeningHours()
{
if (self::$hours == null) {
self::$hours = OpeningHour::all();
}
}
public static function isOpen()
{
self::getOpeningHours();
// get current date
Carbon::now()->format('w');
$open_window = self::$hours->get(Carbon::now()->format('w'));
// is it over current days' opening hours?
if (Carbon::now()->toTimeString() > $open_window->opening_time)
return true;
else
return false;
}
}
像这样,你不必使用构造函数。
另一种方法(可能是更 'Laravelish' 的方法)是将您的 class 迁移到服务提供商中:
https://laravel.com/docs/5.3/providers
寻找 "View composers",它们是将数据注入您的视图的方法。
我正在尝试从我的 blade 视图调用 App\Http\Timeslot::isOpen()
,但我不明白如何在不将 isOpen() 声明为静态的情况下调用它。我不能将它声明为静态的,因为我需要从构造中使用 $this->hours 。如果我不声明它是静态的 laravel returns Cannot redclare Class error
有人可以建议我应该如何编写它以便我仍然可以访问 $this->hours 变量吗?
Blade 模板:
@if(App\Http\Timeslot::isOpen())
We're open
@else
We're closed
@endif
时间段Class
<?php namespace App\Http;
use App\OpeningHour;
use Carbon\Carbon;
class Timeslot
{
protected $hours;
public function __construct()
{
$this->hours = OpeningHour::all();
}
public static function isOpen()
{
// get current date
Carbon::now()->format('w');
$open_window = $this->hours->get(Carbon::now()->format('w'));
// is it over current days' opening hours?
if(Carbon::now()->toTimeString() > $open_window->opening_time)
return true;
else
return false;
}
}
您也可以将 $hours 声明为静态的,并在 isOpen() 中调用方法 retrieve/cache 它们。
类似于:
<?php namespace App\Http;
use App\OpeningHour;
use Carbon\Carbon;
class Timeslot
{
static protected $hours = null;
public static function getOpeningHours()
{
if (self::$hours == null) {
self::$hours = OpeningHour::all();
}
}
public static function isOpen()
{
self::getOpeningHours();
// get current date
Carbon::now()->format('w');
$open_window = self::$hours->get(Carbon::now()->format('w'));
// is it over current days' opening hours?
if (Carbon::now()->toTimeString() > $open_window->opening_time)
return true;
else
return false;
}
}
像这样,你不必使用构造函数。
另一种方法(可能是更 'Laravelish' 的方法)是将您的 class 迁移到服务提供商中: https://laravel.com/docs/5.3/providers
寻找 "View composers",它们是将数据注入您的视图的方法。