1. Go to this page and download the library: Download dcr/framework library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dcr / framework example snippets
declare(strict_types=1);
namespace App\Controller;
use App\Middleware\AuthMiddleware;
use App\Middleware\TestMiddleware;
use App\Service\TestService;
use Dcr\Annotation\Mapping\Middlewares;
use Dcr\Annotation\Mapping\RequestMapping;
use DI\Attribute\Inject;
class MIddlewareController extends Controller
{
#[Inject]
public TestService $testService;
#[RequestMapping(methods: 'GET , POST', path:'/test/middleware')]
#[Middlewares(AuthMiddleware::class, TestMiddleware::class)]
public function test1()
{
// $this->testService->testDi();
return apiResponse([]);
}
}
#[Inject]
public Config $config;
#[RequestMapping(methods: "GET , POST", path:"/test/config")]
public function config()
{
//di()->get(Config::class)->get('app.debug');
return $this->config->get('app.debug');
}
namespace App\Middleware;
use App\Exception\RuntimeException;
use App\Middleware\Contract\MiddlewareInterface;
class TestMiddleware implements MiddlewareInterface
{
public function handle()
{
return static function ($request, $next) {
// throw new RuntimeException('未登录');
$response = $next->handle($request);
return $response;
};
}
}
declare(strict_types = 1);
namespace App\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Toolkit\PFlag\FlagsParser;
class Test extends \Inhere\Console\Command
{
// php artisan test
protected static string $name = 'test';
protected static string $desc = 'print system ENV information';
protected function configFlags(FlagsParser $fs): void
{
// 绑定选项
$fs->addOptByRule('update, up', 'bool;update linux command docs to latest');
$fs->addOptByRule('init, i', 'bool;update linux command docs to latest');
$fs->addOptByRule('search, s', 'string;input keywords for search');
// - 这里没有设置必须 可以不传,获取到就是空string
$fs->addArg('keywords', 'the keywords for search or show docs', 'string');
}
protected function execute(Input $input, Output $output): void
{
$keywords = $this->flags->getOpt('search', 23);
var_dump($keywords);
$output->info('hello world ...');
}
}
declare(strict_types = 1);
namespace App\Event;
use Symfony\Contracts\EventDispatcher\Event;
/**
* Class TestEvent
*/
class TestEvent extends Event
{
public const NAME = 'order.placed';
protected $params;
public function __construct($params)
{
$this->params = $params;
}
public function getParams()
{
return $this->params;
}
}
declare(strict_types = 1);
namespace App\Listener;
use App\Event\TestEvent;
use App\Utils\LogBase;
use Symfony\Contracts\EventDispatcher\Event;
class TestEventListener implements BaseListenerInterface
{
public function process(object $event)
{
echo '打印参数'.PHP_EOL;
var_dump($event->getParams());
}
}