為了提供一個(gè)統(tǒng)一API的實(shí)現(xiàn)方式和使用方式,將會(huì)做出一系列實(shí)現(xiàn)和使用的規(guī)范
API 使用基于令牌的身份驗(yàn)證機(jī)制。某些終端不需要身份驗(yàn)證。您可以通過(guò) /api/token 取得一個(gè)令牌,主要有兩種令牌:
一般情況,后臺(tái)管理程序推薦使用密碼授權(quán)令牌。
namespace Notadd\Foundation\Setting\Controllers;
use Notadd\Foundation\Routing\Abstracts\Controller;
use Notadd\Foundation\Setting\Contracts\SettingsRepository;
use Notadd\Foundation\Setting\Handlers\AllHandler;
use Notadd\Foundation\Setting\Handlers\SetHandler;
/**
* Class ApiController.
*/
class SettingController extends Controller
{
/**
* @var \Notadd\Foundation\Setting\Contracts\SettingsRepository
*/
protected $settings;
/**
* SettingController constructor.
*
* @param \Notadd\Foundation\Setting\Contracts\SettingsRepository $settings
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function __construct(SettingsRepository $settings)
{
parent::__construct();
$this->settings = $settings;
}
/**
* TODO: Method Description
*
* @param \Notadd\Foundation\Setting\Handlers\AllHandler $handler
*
* @return \Notadd\Foundation\Passport\Responses\ApiResponse
* @throws \Exception
*/
public function all(AllHandler $handler)
{
$response = $handler->toResponse();
return $response->generateHttpResponse();
}
/**
* @param \Notadd\Foundation\Setting\Handlers\SetHandler $handler
*
* @return \Notadd\Foundation\Passport\Responses\ApiResponse
* @throws \Exception
*/
public function set(SetHandler $handler)
{
$response = $handler->toResponse();
return $response->generateHttpResponse();
}
}
namespace Notadd\Foundation\Setting\Handlers;
use Illuminate\Container\Container;
use Illuminate\Http\Request;
use Illuminate\Translation\Translator;
use Notadd\Foundation\Passport\Abstracts\DataHandler;
use Notadd\Foundation\Setting\Contracts\SettingsRepository;
/**
* Class SettingHandler.
*/
class AllHandler extends DataHandler
{
/**
* @var \Notadd\Foundation\Setting\Contracts\SettingsRepository
*/
protected $settings;
/**
* AllHandler constructor.
*
* @param \Illuminate\Container\Container $container
* @param \Illuminate\Http\Request $request
* @param \Notadd\Foundation\Setting\Contracts\SettingsRepository $settings
* @param \Illuminate\Translation\Translator $translator
*/
public function __construct(
Container $container,
Request $request,
SettingsRepository $settings,
Translator $translator
) {
parent::__construct($container, $request, $translator);
$this->settings = $settings;
}
/**
* Http code.
*
* @return int
*/
public function code()
{
return 200;
}
/**
* Data for handler.
*
* @return array
*/
public function data()
{
return $this->settings->all()->toArray();
}
/**
* Errors for handler.
*
* @return array
*/
public function errors()
{
return [
'獲取全局設(shè)置失??!',
];
}
/**
* Messages for handler.
*
* @return array
*/
public function messages()
{
return [
'獲取全局設(shè)置成功!',
];
}
}
namespace Notadd\Foundation\Setting\Handlers;
use Illuminate\Container\Container;
use Illuminate\Http\Request;
use Illuminate\Translation\Translator;
use Notadd\Foundation\Passport\Abstracts\SetHandler as AbstractSetHandler;
use Notadd\Foundation\Setting\Contracts\SettingsRepository;
/**
* Class SetHandler.
*/
class SetHandler extends AbstractSetHandler
{
/**
* @var \Notadd\Foundation\Setting\Contracts\SettingsRepository
*/
protected $settings;
/**
* SetHandler constructor.
*
* @param \Illuminate\Container\Container $container
* @param \Illuminate\Http\Request $request
* @param \Notadd\Foundation\Setting\Contracts\SettingsRepository $settings
* @param \Illuminate\Translation\Translator $translator
*/
public function __construct(
Container $container,
Request $request,
SettingsRepository $settings,
Translator $translator
) {
parent::__construct($container, $request, $translator);
$this->settings = $settings;
}
/**
* Data for handler.
*
* @return array
*/
public function data()
{
return $this->settings->all()->toArray();
}
/**
* Errors for handler.
*
* @return array
*/
public function errors()
{
return [
'修改設(shè)置失敗!',
];
}
/**
* Execute Handler.
*
* @return bool
*/
public function execute()
{
$this->settings->set('site.enabled', $this->request->input('enabled'));
$this->settings->set('site.name', $this->request->input('name'));
$this->settings->set('site.domain', $this->request->input('domain'));
$this->settings->set('site.beian', $this->request->input('beian'));
$this->settings->set('site.company', $this->request->input('company'));
$this->settings->set('site.copyright', $this->request->input('copyright'));
$this->settings->set('site.statistics', $this->request->input('statistics'));
return true;
}
/**
* Messages for handler.
*
* @return array
*/
public function messages()
{
return [
'修改設(shè)置成功!',
];
}
}
更多建議: