PHP code example of lugege / laravel-aspect

1. Go to this page and download the library: Download lugege/laravel-aspect 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/ */

    

lugege / laravel-aspect example snippets


use Lugege\LaravelAspect\EasyRouter;

EasyRouter::register();

use Lugege\LaravelAspect\Attributes\Routes\GetMapping;
use Lugege\LaravelAspect\Attributes\Routes\Prefix;

#[Prefix(path: '/user')]
class UserController
{
    #[GetMapping(path: '/login')] 
    public function login() {}
}


use Lugege\LaravelPlus\Attributes\Service;

#[Service]
class UserService
{
    public function register() {}
}


use Lugege\LaravelPlus\Traits\Injectable;

class UserController
{
    use Injectable;
  
    #[Autowired]
    private UserService $userService;
    
    public function register()
    {
        $this->userService->register(); 
    }
}


use Lugege\LaravelPlus\Traits\Injectable;
use Lugege\LaravelPlus\Attributes\Value;

class DatabaseService
{
    use Injectable;
    
    #[Value('database.connections.mysql.host', 'localhost')]
    private string $dbHost;
    
    #[Value('database.connections.mysql.port', 3306)]
    private int $dbPort;
    
    #[Value('app.timezone', 'UTC')]
    private string $timezone;
    
    public function connect()
    {
        // 使用注入的配置值
        dump("Connecting to {$this->dbHost}:{$this->dbPort}");
    }
}

use Lugege\LaravelPlus\Bean;

/**
 * @method getUsername()
 * @method setUsername()
 * @method getPassword()
 * @method setPassword()
 */
class RegisterParams extends Bean
{
    protected string $username;
    
    protected string $password;
}

new RegisterParams(['username' => 'bob', 'password' => 'passw0rd']);

use Lugege\LaravelPlus\Bean;

$bean = new Bean();
class Bar extends Bean {  
    // 一些属性  
}
Bar::fromBean($bean)

use Lugege\LaravelPlus\Bean;

$bean = new Bean();
$arr = $bean->toArray(usingSnakeCase: false);
$json = $bean->toJson(usingSnakeCase: true);

use Lugege\LaravelPlus\Bean;
(new Bean())->equals($bean2);

use Lugege\LaravelPlus\Bean;
use Lugege\LaravelPlus\Attributes\TypeConverter;

class User extends Bean
{
    #[TypeConverter(value: BoolConverter::class)]
    protected BoolEnum $isGuest;
}

class BoolConverter
{
    public function convert(bool|string $value): BoolEnum
    {
        if ($value === 'YES' || $value === 'yes' || $value === 'y' || $value === 'Y') {
            return BoolEnum::TRUE;
        }
        if ($value === 'NO' || $value === 'no' || $value === 'N' || $value === 'n') {
            return BoolEnum::FALSE;
        }

        return $value ? BoolEnum::TRUE : BoolEnum::FALSE;
    }
}

use Lugege\LaravelPlus\Bean;

class User extends Bean
{
    protected Company $company;
}

class Company extends Bean
{
    protected string $name;
}

use Lugege\LaravelPlus\Bean;
use Lugege\LaravelPlus\Attributes\BeanList;

/**
 * @method Company[] getCompanies()
 */
class User extends Bean
{
    /**
     * @var Company[]
     */
    #[BeanList(value: Company::class)]
    protected array $companies;
}

$user = new User(['companies' => [['name' => 'Lugege'], ['name' => 'Google']]]);
foreach ($user->getCompanies() as $company) {
    dump($company->getName());
}