Laravel HyperfLaravel Hyperf
Laravel Hyperf
Documentation
GitHub
Laravel Hyperf
Documentation
GitHub
Laravel Hyperf

Laravel Hyperf

A Laravel-Style PHP Framework for Web Artisans.

Get StartedGitHub

Laravel Friendly

By porting Laravel's core infrastructure and fundamental components, this framework enables Laravel developers to quickly adapt to it.

High Performance

Leveraging Swoole's native coroutine support, it delivers exceptional performance and efficient concurrency handling.

Ecosystem Compatibility

Laravel Hyperf is compatible with the Hyperf ecosystem, sharing the same community resources and packages.

Coding Like in Laravel

Laravel artisans can enjoy a familiar development experience that mirrors the original framework. The simple, elegant syntax remains intact, enabling developers to stay productive while leveraging enhanced performance.

  • Authentication
  • Authorization
  • Eloquent ORM
  • Database Migrations
  • Validation
  • Notification and Email
  • File Storage
  • Job Queues
  • Task Scheduling
  • Testing
  • Events

Authentication

Authenticating users is as simple as adding an authentication middleware to your Laravel Hyperf route definition:

Route::get('/profile', ProfileController::class, [
  'middleware' => 'auth'
]);

Once the user is authenticated, you can access the authenticated user via the Auth facade:

use LaravelHyperf\Support\Facades\Auth;

$user = Auth::user();

Of course, you may define your own authentication middleware, allowing you to customize the authentication process.

Read Authentication docs

Authorization

You'll often need to check whether an authenticated user is authorized to perform a specific action. Laravel Hyperf's model policies make it a breeze:

php artisan make:policy UserPolicy

Once you've defined your authorization rules in the generated policy class, you can authorize the user's request in your controller methods:

public function update(Request $request, Invoice $invoice)
{
    Gate::authorize('update', $invoice);

    $invoice->update(/* ... */);
}

Read Authorization docs

Eloquent

Scared of databases? Don't be. Laravel Hyperf’s Eloquent ORM makes it painless to interact with your application's data, and models, migrations, and relationships can be quickly scaffolded:

php artisan make:model Invoice --migration

Once you've defined your model structure and relationships, you can interact with your database using Eloquent's powerful, expressive syntax:

// Create a related model...
$user->invoices()->create(['amount' => 100]);

// Update a model...
$invoice->update(['amount' => 200]);

// Retrieve models...
$invoices = Invoice::unpaid()->where('amount', '>=', 100)->get();

// Rich API for model interactions...
$invoices->each->pay();

Read Eloquent docs

Database Migrations

Migrations are like version control for your database, allowing your team to define and share your application's database schema definition:

return new class extends Migration {
    public function up()
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->uuid()->primary();
            $table->foreignUuid('airline_id')->constrained();
            $table->string('name');
            $table->timestamps();
        });
    }
};

Read Migration docs

Validation

Laravel has over 90 powerful, built-in validation rules and, using Laravel Hyperf Precognition, can provide live validation on your frontend:

public function update(Request $request)
{
    $validated = $request->validate([
        'email' => 'required|email|unique:users',
        'password' => Password::required()->min(8),
    ]);

    $request->user()->update($validated);
}

Read Validation docs

Notifications & Mail

Use Laravel Hyperf to quickly send beautifully styled notifications to your users via email, Slack, SMS, in-app, and more:

php artisan make:notification InvoicePaid

Once you have generated a notification, you can easily send the message to one of your application's users:

$user->notify(new InvoicePaid($invoice));

Read Notification and Mail docs

File Storage

Laravel Hyperf provides a robust filesystem abstraction layer, providing a single, unified API for interacting with local filesystems and cloud based filesystems like Amazon S3:

$path = Storage::disk('s3')
    ->put('avatars/1', $request->file('avatar'));

Regardless of where your files are stored, interact with them using Laravel Hyperf's simple, elegant syntax:

$content = Storage::get('photo.jpg');

Storage::put('photo.jpg', $content);

Read File Storage docs

Job Queues

Laravel Hyperf lets you to offload slow jobs to a background queue, keeping your web requests snappy:

$podcast = Podcast::create(/* ... */);

ProcessPodcast::dispatch($podcast)->onQueue('podcasts');

You can run as many queue workers as you need to handle your workload:

php artisan queue:work redis --queue=podcasts

Read Queues docs

Task Scheduling

Schedule recurring jobs and commands with an expressive syntax and say goodbye to complicated configuration files:

$schedule->job(NotifySubscribers::class)->hourly();

Laravel Hyperf's scheduler can even handle multiple servers and offers built-in overlap prevention:

$schedule->job(NotifySubscribers::class)
    ->dailyAt('9:00')
    ->onOneServer()
    ->withoutOverlapping();

Read Task Scheduling docs

Testing

Laravel Hyperf is built for testing. From unit tests to feature tests, you’ll feel more confident in deploying your application:

class RefreshDatabaseTest extends TestCase
{
    use RefreshDatabase;

    public function testCreateUser()
    {
        $user = factory(User::class)->create();

        $this->assertDatabaseHas('users', [
            'id' => $user->id,
        ]);
    }
}

Read Testing docs

Events & Websockets

Laravel Hyperf's events allow you to send and listen for events across your application, and listeners can easily be dispatched to a background queue:

OrderShipped::dispatch($order);
class SendShipmentNotification implements ShouldQueue
{
    public function handle(OrderShipped $event): void
    {
        // ...
    }
}

Your frontend application can even subscribe to your Laravel Hyperf events using Laravel Echo and WebSockets, allowing you to build real-time, dynamic applications:

Echo.private(`orders.${orderId}`)
    .listen('OrderShipped', (e) => {
        console.log(e.order);
    });

Read Events docs

Frequently Asked Questions

Is Laravel Hyperf compatible with Laravel packages?

While Laravel Hyperf maintains the similar development experience like Laravel, you can't install Laravel packages on this framework due to the fundamental differences in architecture. However, many Laravel concepts and patterns can be easily adapted for use with Laravel Hyperf.

How does Laravel Hyperf achieve high performance?

Laravel Hyperf achieves high performance through Swoole's coroutine system, which enables non-blocking I/O operations and efficient concurrency handling. This allows the framework to handle more concurrent connections with fewer resources compared to traditional PHP-FPM.

Why not use Octane directly?

Octane accelerates Laravel by maintaining the framework in a persistent application state, resetting request-scoped resources between requests. However, it doesn't introduce non-blocking I/O capabilities to Laravel's architecture. Furthermore, all components in Laravel weren't designed with coroutines in mind. It will cause states pollution while context switching among coroutines.

For I/O-intensive scenarios, even with Octane's improvements, your application's ability to handle concurrent requests is still limited by the duration of these I/O operations.

MIT Licensed | Copyright © 2024-present Laravel Hyperf