PHP code example of yabasi / framework

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

    

yabasi / framework example snippets


$router->get('/', function() {
    return 'Hello Yabasi!';
});

$router->get('/users/{id}', 'UserController@show');

$router->group(['middleware' => [\Yabasi\Middleware\SessionMiddleware::class]], function ($router) {
   $router->get('/account', 'AccountController@index');
   $router->post('/account/update', 'AccountController@update');
});

namespace App\Controllers;

use Yabasi\Http\Request;
use Yabasi\Http\Response;

class UserController extends Controller
{
    public function show(Request $request, $id)
    {
        $user = User::find($id);
        return $this->view('users.show', ['user' => $user]);
    }
}

// Define a model
class User extends Model
{
    protected static string $table = 'users';

    public function posts()
    {
        return $this->hasMany(Post::class);
    }
}

// Retrieve all users
$users = User::all();

// Find a specific user
$user = User::find(1);

// Create a new user
$user = new User();
$user->name = 'John Doe';
$user->email = '[email protected]';
$user->save();

// Update a user
$user = User::find(1);
$user->name = 'Jane Doe';
$user->save();

// Delete a user
$user = User::find(1);
$user->delete();

// Query building
$users = User::where('active', true)
             ->orderBy('name', 'asc')
             ->limit(10)
             ->get();

// Relationships
$user = User::find(1);
$posts = $user->posts;

namespace App\Middleware;

use Closure;
use Yabasi\Http\Request;
use Yabasi\Http\Response;

class AuthMiddleware implements MiddlewareInterface
{
    public function handle(Request $request, Closure $next): Response
    {
        if (!$request->session->has('user_id')) {
            return redirect('/login');
        }
        return $next($request);
    }
}

namespace App\Commands;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class GenerateReportCommand extends Command
{
    protected static $defaultName = 'app:generate-report';

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('Generating report...');
        // Report generation logic here
        $output->writeln('Report generated successfully!');
        return Command::SUCCESS;
    }
}

namespace App\Providers;

use Yabasi\ServiceProvider\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->container->singleton(MyService::class, function ($container) {
            return new MyService($container->get(DependencyClass::class));
        });
    }

    public function boot(): void
    {
        // Boot logic here
    }
}

// storage/lang/en.json
return [
    'welcome' => 'Welcome to our application!',
    'goodbye' => 'Goodbye, see you soon!',
];

// resources/lang/es.json
return [
    'welcome' => '¡Bienvenido a nuestra aplicación!',
    'goodbye' => '¡Adiós, hasta pronto!',
];

echo __('messages.welcome');

$validator = new Validator($request->all(), [
    'name' => ' => 'thErrors($validator);
}

// Set a value in the cache
Cache::set('key', 'value', 3600);

// Get a value from the cache
$value = Cache::get('key', 'default');

// Remove a value from the cache
Cache::delete('key');

// Check if a key exists in the cache
if (Cache::has('key')) {
    // ...
}

// Dispatch an event
$dispatcher = new EventDispatcher();
$dispatcher->dispatch(new UserRegisteredEvent($user));

// Listen for an event
$dispatcher->listen(UserRegisteredEvent::class, function($event) {
    // Handle the event
});

php yabasi make:migration create_users_table

use Yabasi\Database\Connection;
use Yabasi\Database\Migrations\MigrationInterface;

class CreateUsersTable implements MigrationInterface
{
    public function up(Connection $connection): void
    {
        $connection->schema()->create('users', function ($table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamps();
        });
    }

    public function down(Connection $connection): void
    {
        $connection->schema()->dropIfExists('users');
    }
}

php yabasi migrate

namespace App\WebSockets;

use Ratchet\ConnectionInterface;
use Yabasi\WebSocket\BaseWebSocketServer;

class ChatServer extends BaseWebSocketServer
{
    public function onMessage(ConnectionInterface $from, $msg)
    {
        foreach ($this->clients as $client) {
            $client->send($msg);
        }
    }
}

php yabasi websocket:serve

namespace App\Jobs;

use Yabasi\Queue\Job;

class SendEmailJob extends Job
{
    protected $email;

    public function __construct($email)
    {
        $this->email = $email;
    }

    public function handle()
    {
        // Send email logic here
    }
}

$queueManager->push(new SendEmailJob('[email protected]'));

php yabasi queue:work

vendor/bin/phpunit
bash
php yabasi app:generate-report