PHP code example of marko / blog

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

    

marko / blog example snippets


use Marko\Core\Attributes\Preference;

#[Preference(PostRepositoryInterface::class)]
class MyPostRepository implements PostRepositoryInterface
{
    // custom implementation
}

use Marko\Core\Attributes\Plugin;
use Marko\Core\Attributes\Before;
use Marko\Core\Attributes\After;

#[Plugin(target: PostService::class)]
class PostServicePlugin
{
    /**
     * The method name matches the target method. #[Before] determines timing.
     * Return null to pass through, an array to modify arguments, or a non-null
     * non-array value to short-circuit and skip the original method.
     */
    #[Before]
    public function createPost(array $data): null|array
    {
        // Modify the data before createPost runs by returning an array
        $data['slug'] = strtolower(trim($data['slug']));

        return [$data];
    }
}

#[Plugin(target: PostService::class)]
class PostServiceAuditPlugin
{
    #[After]
    public function createPost(Post $post): Post
    {
        // Act on the result after createPost runs
        return $post;
    }
}

use Marko\Core\Attributes\Observer;
use Marko\Blog\Events\Post\PostCreated;

#[Observer(event: PostCreated::class)]
class PostCreatedObserver
{
    public function handle(PostCreated $event): void
    {
        // react to event — send notification, update cache, etc.
    }
}