PHP code example of mongoose-studio / phobos-framework
1. Go to this page and download the library: Download mongoose-studio/phobos-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/ */
mongoose-studio / phobos-framework example snippets
define('ROOT', dirname(__DIR__)); // raíz del proyecto
define('APPLICATION', ROOT . '/app'); // dir de la app (opcional; por defecto ROOT/app)
) // lee ROOT/config
->bootstrap(App\AppModule::class);
$response = $app->run();
$response->send();
namespace App;
use Phobos\Module\ModuleInterface;
use Phobos\Routing\Router;
class AppModule implements ModuleInterface
{
public function routes(Router $router): void
{
$router->get('/', [HomeController::class, 'index']);
$router->group(['prefix' => 'api'], function($r) {
$r->get('/users', [UserController::class, 'index']);
$r->get('/users/:id', [UserController::class, 'show']);
$r->post('/users', [UserController::class, 'store']);
});
}
public function middlewares(): array
{
return []; // Middlewares globales
}
public function providers(): array
{
return [DatabaseProvider::class, AuthProvider::class];
}
}
namespace App\Controllers;
use Phobos\Http\Request;
use Phobos\Http\Response;
class UserController
{
public function __construct(
private UserRepository $users,
private Logger $logger
) {}
public function index(Request $request): Response
{
$users = $this->users->all();
return Response::json($users);
}
public function show(Request $request, string $id): Response
{
$user = $this->users->find($id);
if (!$user) {
return Response::error('Usuario no encontrado', 404);
}
return Response::json($user);
}
public function store(Request $request): Response
{
$data = $request->json();
$user = $this->users->create($data);
return Response::json($user, 201);
}
}
$host = config('database.connections.mysql.host');
$default = config('database.default');
// Establecer en tiempo de ejecución
Config::set('app.timezone', 'America/Santiago');
namespace App\Providers;
use Phobos\Core\ServiceProvider;
use Phobos\Core\Container;
class DatabaseProvider extends ServiceProvider
{
public function register(Container $container): void
{
$container->singleton(Database::class, function($c) {
return new Database(
config('database.connections.mysql')
);
});
}
public function boot(Container $container): void
{
// Ejecutado después de que todos los providers se registren
$db = $container->make(Database::class);
$db->connect();
}
}
use Phobos\Exceptions\NotFoundException;
use Phobos\Exceptions\UnauthorizedException;
use Phobos\Exceptions\ValidationException;
// Lanzar excepciones
throw new NotFoundException('Usuario no encontrado');
throw new UnauthorizedException('Token inválido');
throw new ValidationException('Datos inválidos', [
'email' => 'El email es requerido'
]);
// Helper rápido
abort(404, 'Recurso no encontrado');
abort(401);
// Acceso al framework
$app = phobos();
$container = container();
$req = request();
// DI
$logger = inject(Logger::class);
// Config/Env
$debug = env('APP_DEBUG');
$timezone = config('app.timezone');
// Paths
$base = base_path('app/Controllers');
$config = config_path('database.php');
$public = public_path('assets/logo.png');
$url = url('/api/users');
// Debugging
dd($variable); // Dump and die
dump($data); // Dump
dpre($array); // Dump con <pre>
// Utilidades
$result = tap($user, fn($u) => $u->save());
$value = value($callback);
if (blank($value)) { }
if (filled($value)) { }
// Ambiente
if (is_dev()) { }
if (is_prod()) { }
namespace PhobosFramework\Tests\Unit;
use PHPUnit\Framework\TestCase;
use PhobosFramework\Core\Container;
class MyTest extends TestCase
{
private Container $container;
protected function setUp(): void
{
$this->container = new Container();
}
public function test_can_bind_service(): void
{
// Arrange
$this->container->bind(MyService::class);
// Act
$service = $this->container->make(MyService::class);
// Assert
$this->assertInstanceOf(MyService::class, $service);
}
}