PHP code example of watish / watishweb

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

    

watish / watishweb example snippets




namespace Watish\WatishWEB\Controller;

use Watish\Components\Attribute\Path;
use Watish\Components\Struct\Request;

class HelloController
{
    #[Path('/')]
    public function index(Request $request) :array
    {
        return [
            "msg" => "hello world"
        ];
    }
}

...
"register_route_auto" => true
...

#[Prefix(string $prefix)]

#[Path(string $path,array $methods)]



namespace Watish\WatishWEB\Controller;

use Watish\Components\Attribute\Middleware;
use Watish\Components\Attribute\Path;
use Watish\Components\Attribute\Prefix;
use Watish\Components\Struct\Request;
use Watish\WatishWEB\Middleware\TestMiddleware;

#[Prefix('/hello')]
class HelloController
{
    #[Path('/index')]
    public function index(Request $request) :array
    {
        return [
            "msg" => "hello world"
        ];
    }

    #[Path('/user/{name}',['GET','POST'])]
    #[Middleware([TestMiddleware::class])]
    public function msg(Request $request) :array
    {
        return [
            "msg" => "hello ".$request->route('name')
        ];
    }
}




use Watish\Components\Includes\Route;
use Watish\WatishWEB\Controller\HelloController;

function do_register_global_middleware(Route $route):void
{
    /**
    $route->register_global_middleware(CorsMiddleware::class);
     */
}

function do_register_routes(Route $route): void
{
    $route->register('/hello/index',[HelloController::class,'index'],[],[]);
    $route->register('/hello/user/{name}',[HelloController::class,'msg'],[TestMiddleware:class],['GET','POST']);
}

Watish\Components\Includes\Route->register(string $path , array $callback , array $before_middlewares , array $methods )



namespace Watish\WatishWEB\Middleware;

use Watish\Components\Attribute\GlobalMiddleware;
use Watish\Components\Struct\Request;
use Watish\Components\Struct\Response;

#[GlobalMiddleware]
class CorsMiddleware implements MiddlewareInterface
{
    public function handle(Request $request,Response $response): void
    {
        $response->header("Access-Control-Allow-Origin", "*");
        $response->header("Access-Control-Allow-Credentials", true);
    }
}



use Watish\Components\Includes\Route;
use Watish\WatishWEB\Controller\HelloController;
use Watish\WatishWEB\Middleware\CorsMiddleware;

function do_register_global_middleware(Route $route):void
{
    $route->register_global_middleware(CorsMiddleware::class);
}

function do_register_routes(Route $route): void
{
    $route->register('/hello/index',[HelloController::class,'index'],[],[]);
    $route->register('/hello/user/{name}',[HelloController::class,'msg'],[],['GET','POST']);
}

#[Middleware(array $middlewares)]



namespace Watish\WatishWEB\Middleware;

use Watish\Components\Struct\Request;
use Watish\Components\Struct\Response;

class TestMiddleware implements MiddlewareInterface
{
    public function handle(Request $request, Response $response)
    {
        $response->header("test","test");
    }
}



namespace Watish\WatishWEB\Controller;

use Watish\Components\Attribute\Middleware;
use Watish\Components\Attribute\Path;
use Watish\Components\Attribute\Prefix;
use Watish\Components\Struct\Request;
use Watish\WatishWEB\Middleware\TestMiddleware;

#[Prefix('/hello')]
class HelloController
{
    #[Path('/index')]
    #[Middleware([TestMiddleware::class])]
    public function index(Request $request) :array
    {
        return [
            "msg" => "hello world"
        ];
    }

    #[Path('/user/{name}',['GET','POST'])]
    #[Middleware([TestMiddleware::class])]
    public function msg(Request $request) :array
    {
        return [
            "msg" => "hello ".$request->route('name')
        ];
    }
}



namespace Watish\WatishWEB\Controller;

use Watish\Components\Attribute\Middleware;
use Watish\Components\Attribute\Path;
use Watish\Components\Attribute\Prefix;
use Watish\Components\Struct\Request;
use Watish\WatishWEB\Middleware\TestMiddleware;

#[Prefix('/hello')]
#[Middleware([TestMiddleware::class])]
class HelloController
{
    #[Path('/index')]
    public function index(Request $request) :array
    {
        return [
            "msg" => "hello world"
        ];
    }

    #[Path('/user/{name}',['GET','POST'])]
    public function msg(Request $request) :array
    {
        return [
            "msg" => "hello ".$request->route('name')
        ];
    }
}



namespace Watish\WatishWEB\Controller;

use Watish\Components\Attribute\Inject;
use Watish\Components\Attribute\Middleware;
use Watish\Components\Attribute\Path;
use Watish\Components\Attribute\Prefix;
use Watish\Components\Struct\Request;
use Watish\WatishWEB\Middleware\TestMiddleware;
use Watish\WatishWEB\Service\BaseService;

#[Prefix('/hello')]
#[Middleware([TestMiddleware::class])]
class HelloController
{
    #[Inject(BaseService::class)]
    public BaseService $baseService;

    #[Path('/index')]
    public function index(Request $request) :array
    {
        return [
            "msg" => $this->baseService->toArray(["Hello",'World'])
        ];
    }

    #[Path('/user/{name}',['GET','POST'])]
    public function msg(Request $request) :array
    {
        return [
            "msg" => "hello ".$request->route('name')
        ];
    }
}



namespace Watish\WatishWEB\Service;

use Watish\Components\Attribute\Async;
use Watish\Components\Attribute\Inject;
use Watish\Components\Utils\Logger;

class TestService
{
    #[Inject(BaseService::class)]
    public BaseService $baseService;
    
    #[Async]
    public function asyncHello(): void
    {
        Logger::info("Hello");
    }
    
    public function hello(string $name) :string
    {
        return "hello {$name}";
    }
}



namespace Watish\WatishWEB\Command;

use Watish\Components\Attribute\Command;
use Watish\Components\Utils\Logger;

#[Command("hello","command")]
class HelloCommand implements CommandInterface
{
    public function handle(): void
    {
        Logger::info("Hello");
    }

}

Command(string $command , string $prefix = "command")



namespace Watish\WatishWEB\Task;

use Watish\Components\Attribute\Crontab;
use Watish\Components\Utils\Logger;

#[Crontab("* * * * *")]
class HelloTask implements TaskInterface
{
    public function execute(): void
    {
        Logger::info("Hello","HelloTask");
    }
}

Crontab(string $rule)
shell
swoole-cli ./bin/CoServer.php

php ./bin/CoServer.php
shell
swoole-cli ./bin/CoServer.php command:hello

php ./bin/CoServer.php command:hello