PHP code example of webfactory / object-routing

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

    

webfactory / object-routing example snippets




use JMS\ObjectRouting\Attribute\ObjectRoute;

// multiple `#[ObjectRoute]` attributes are possible for different `type`s 
#[ObjectRoute(type: 'view', name: 'the-actual-route-name', params: ['slug' => 'slug'])
class BlogPost
{
    public function getSlug(): string
    {
        // ...
    }
}



use Symfony\Component\Routing\Attribute\Route;
// ...

class BlogPostController
{
    #[Route('/blog-posts/{slug}', name: 'the-actual-route-name')]
    public function viewAction(BlogPost $post): Response
    {
        // ...
    }
}

$objectRouter->generate('view', $blogPost);
// equivalent to
$router->generate('the-actual-route-name', ['slug' => $blogPost->getSlug()]);

use JMS\ObjectRouting\Attribute\ObjectRoute;

#[ObjectRoute(type: 'detail', name: 'app_talk_detail', params: ['id' => 'id'])
class Talk {
    // ...
}

#[ObjectRoute(type: 'detail', name: 'app_workshop_detail', params: ['slug' => 'slug'])
class Workshop {
    // ...
}