Skip to content

feat(laravel): laravel component #5882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jun 25, 2024
Merged

feat(laravel): laravel component #5882

merged 18 commits into from
Jun 25, 2024

Conversation

soyuka
Copy link
Member

@soyuka soyuka commented Oct 12, 2023

Laravel support in API Platform

Note that this is still in development there are a few things missing (see todolist)

Must do:

Some ideas:

  • providers/processors as middlewares
  • add the graphQL support? Not a priority yet as lighthouse-php is generally used, we may see in the future how we could integrate with lighthouse

To try this:

  1. create a laravel project:
composer create-project laravel/laravel my-api
  1. update your composer.json with:
    "repositories": [
        {
            "type": "path",
            "url": "/path/to/core/src/Laravel"
        }
    ]
}
  1. composer update, I added #[ApiResource] on the model.

Sorry, something went wrong.

@kilwir76
Copy link

not working for the moment.
I haven't package api-platform who install after create project laravel and change composer.json

Copy link

stale bot commented Jan 6, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jan 6, 2024
@dunglas dunglas removed the stale label Jan 7, 2024
@JustSteveKing
Copy link

Is there a specific way that we need to add serialization support for other types other than jsonld? I tried both jsonapi and jsonhal and get the following trace:

[2024-04-26 20:34:38] local.ERROR: Serialization for the format "jsonapi" is not supported. {"exception":"[object] (Symfony\\Component\\Serializer\\Exception\\UnsupportedFormatException(code: 0): Serialization for the format \"jsonapi\" is not supported. at /Users/steve/Herd/laravel-api-platform/vendor/symfony/serializer/Serializer.php:134)
[stacktrace]
#0 /Users/steve/Herd/core/src/State/Processor/SerializeProcessor.php(65): Symfony\\Component\\Serializer\\Serializer->serialize(Object(ApiPlatform\\Laravel\\Eloquent\\Paginator), 'jsonapi', Array)
#1 /Users/steve/Herd/core/src/State/Processor/WriteProcessor.php(42): ApiPlatform\\State\\Processor\\SerializeProcessor->process(Object(ApiPlatform\\Laravel\\Eloquent\\Paginator), Object(ApiPlatform\\Metadata\\GetCollection), Array, Array)
#2 /Users/steve/Herd/core/src/Hydra/State/HydraLinkProcessor.php(52): ApiPlatform\\State\\Processor\\WriteProcessor->process(Object(ApiPlatform\\Laravel\\Eloquent\\Paginator), Object(ApiPlatform\\Metadata\\GetCollection), Array, Array)
#3 /Users/steve/Herd/core/src/Laravel/Controller/ApiPlatformController.php(90): ApiPlatform\\Hydra\\State\\HydraLinkProcessor->process(Object(ApiPlatform\\Laravel\\Eloquent\\Paginator), Object(ApiPlatform\\Metadata\\GetCollection), Array, Array)
#4 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): ApiPlatform\\Laravel\\Controller\\ApiPlatformController->__invoke(Object(Illuminate\\Http\\Request))
#5 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(43): Illuminate\\Routing\\Controller->callAction('__invoke', Array)
#6 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Routing/Route.php(259): Illuminate\\Routing\\ControllerDispatcher->dispatch(Object(Illuminate\\Routing\\Route), Object(ApiPlatform\\Laravel\\Controller\\ApiPlatformController), '__invoke')
#7 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Routing/Route.php(205): Illuminate\\Routing\\Route->runController()
#8 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Routing/Router.php(806): Illuminate\\Routing\\Route->run()
#9 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(144): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#10 /Users/steve/Herd/core/src/Laravel/ApiPlatformMiddleware.php(38): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#11 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): ApiPlatform\\Laravel\\ApiPlatformMiddleware->handle(Object(Illuminate\\Http\\Request), Object(Closure), '_api_/api/books...')
#12 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#13 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Routing/Router.php(805): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#14 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Routing/Router.php(784): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))
#15 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Routing/Router.php(748): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route))
#16 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Routing/Router.php(737): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))
#17 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(200): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request))
#18 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(144): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request))
#19 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#20 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php(31): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#21 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#22 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#23 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php(40): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#24 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\TrimStrings->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#25 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#26 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#27 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(99): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#28 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#29 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Http/Middleware/HandleCors.php(62): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#30 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Http\\Middleware\\HandleCors->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#31 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Http/Middleware/TrustProxies.php(39): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#32 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Http\\Middleware\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#33 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#34 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(175): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#35 /Users/steve/Herd/laravel-api-platform/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(144): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))
#36 /Users/steve/Herd/laravel-api-platform/public/index.php(51): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#37 /Applications/Herd.app/Contents/Resources/valet/server.php(151): require('/Users/steve/He...')
#38 {main}
"} 

@soyuka
Copy link
Member Author

soyuka commented Apr 27, 2024

https://github.com/api-platform/core/pull/5882/files#diff-1f34d8685c9bbcc2037ef47c41cb6dac916a0fe6fc06e2047b9635e35fa13527R396 it's missing jsonapi/hal normalizers indeed.

@JustSteveKing
Copy link

https://github.com/api-platform/core/pull/5882/files#diff-1f34d8685c9bbcc2037ef47c41cb6dac916a0fe6fc06e2047b9635e35fa13527R396 it's missing jsonapi/hal normalizers indeed.

I assume you know how to do this? I am still getting up to speed with some of these things!

@soyuka
Copy link
Member Author

soyuka commented May 24, 2024

Indeed, I plan to continue working on this mid-june.

@soyuka soyuka force-pushed the laravel branch 5 times, most recently from aab48a6 to 346b164 Compare June 18, 2024 09:58

Verified

This commit was signed with the committer’s verified signature.
soyuka Antoine Bluchet

Verified

This commit was signed with the committer’s verified signature.
soyuka Antoine Bluchet

$this->app->bind(SchemaFactoryInterface::class, JsonApiSchemaFactory::class);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at some point we may want to not load these when jsonapi format is not enabled, for now it doesn't impact the runtime much I propose to leave that to future improvement.

soyuka added 2 commits June 19, 2024 12:40

Verified

This commit was signed with the committer’s verified signature.
soyuka Antoine Bluchet

Verified

This commit was signed with the committer’s verified signature.
soyuka Antoine Bluchet
soyuka added 6 commits June 19, 2024 18:43

Verified

This commit was signed with the committer’s verified signature.
soyuka Antoine Bluchet

Verified

This commit was signed with the committer’s verified signature.
soyuka Antoine Bluchet

Verified

This commit was signed with the committer’s verified signature.
soyuka Antoine Bluchet

Verified

This commit was signed with the committer’s verified signature.
soyuka Antoine Bluchet

Verified

This commit was signed with the committer’s verified signature.
soyuka Antoine Bluchet

Verified

This commit was signed with the committer’s verified signature.
soyuka Antoine Bluchet
soyuka added 5 commits June 24, 2024 11:11

Verified

This commit was signed with the committer’s verified signature.
soyuka Antoine Bluchet

Verified

This commit was signed with the committer’s verified signature.
soyuka Antoine Bluchet

Verified

This commit was signed with the committer’s verified signature.
soyuka Antoine Bluchet

Verified

This commit was signed with the committer’s verified signature.
soyuka Antoine Bluchet

Verified

This commit was signed with the committer’s verified signature.
soyuka Antoine Bluchet
@soyuka soyuka marked this pull request as ready for review June 24, 2024 13:43

Verified

This commit was signed with the committer’s verified signature.
soyuka Antoine Bluchet
@soyuka soyuka force-pushed the laravel branch 4 times, most recently from 502a98d to d738493 Compare June 24, 2024 15:09

Verified

This commit was signed with the committer’s verified signature.
soyuka Antoine Bluchet
@soyuka soyuka force-pushed the laravel branch 2 times, most recently from 3467ff6 to f5c2c85 Compare June 25, 2024 13:43

Verified

This commit was signed with the committer’s verified signature.
soyuka Antoine Bluchet
@soyuka soyuka merged commit 0d5f356 into api-platform:main Jun 25, 2024
78 of 81 checks passed
@soyuka
Copy link
Member Author

soyuka commented Jun 25, 2024

merging now that the CI is all green, it's still missing some features (security / filters) and it'll be easier to review afterwards.

soyuka added a commit to DartCZ/core that referenced this pull request Jun 27, 2024

Verified

This commit was signed with the committer’s verified signature.
soyuka Antoine Bluchet
* feat(laravel): laravel component

* try to skip laravel

* feat(jsonapi): component

* feat(laravel): json api support (needs review)

* work on relations

* relations (needs toMany) + skolem + IRI to resource

* links handler

* ulid

* validation

* slug post

* remove deprecations

* move classes

* fix tests

* fix tests metadata

* phpstan

* missing class

* fix laravel tests

* fix stan
@shahzaibtariqbutt
Copy link

Could we get an example usage with laravel model? Specially the normalizationContext, denormalizationContext stuff.

Heres what I am trying to do?

<?php

declare(strict_types=1);

namespace App\Models;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Post;
use App\Api\Processor\PushToken\PushTokenRegisterProcessor;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

#[ApiResource(
    operations: [
        new Post(
            '/push-tokens',
            processor: PushTokenRegisterProcessor::class,
            normalizationContext: ['groups' => ['read']],
            denormalizationContext: ['groups' => ['write']]
        ),
    ]
)]
class PushToken extends Model
{
    use HasUlids;

    public $timestamps = false;

    protected $apiProperties = [
        'fcm' => [
            'groups' => [
                'read',
                'write',
            ]
        ],
        'apns' => [
            'groups' => [
                'read',
                'write',
            ]
        ],
    ];

    protected $fillable = [
        'user_id',
        'fcm',
        'apns',
        'last_registered_at',
        'delivery_failed_at',
    ];

    protected $casts = [
        'last_registered_at' => 'datetime',
        'delivery_failed_at' => 'datetime',
    ];

    /**
     * @return BelongsTo<User, $this>
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /** @return Collection<int, $this> */
    public static function getUserTokens(User $user): Collection
    {
        return self::query()
            ->where('user_id', $user->id)
            ->get();
    }

    public function isFcm(): bool
    {
        return null !== $this->fcm;
    }

    public function isApns(): bool
    {
        return null !== $this->apns;
    }
}

@soyuka
Copy link
Member Author

soyuka commented Feb 7, 2025

@shahzaibtariqbutt check https://github.com/api-platform/laravel-demo/pull/2/files and please open a new issue is you need, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants