DEV Community

dvaknheo
dvaknheo

Posted on

I port Symfony Demo to DuckPHP, and it be simple

A controller with 18 use statements, cut down to 3

I did something a bit stubborn: I rebuilt the official Symfony Demo, from scratch, with DuckPHP.

Not as an exercise — to test one claim: a lot of PHP application complexity is not business complexity, it is framework concept tax.

Start with one screen of code. The original BlogController header:

use App\Entity\Post;
use App\Form\CommentType;
use App\Repository\PostRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
// ... 18 lines in total
Enter fullscreen mode Exit fullscreen mode

Mine:

use DuckSymfonyDemo\Business\BlogBusiness;
use DuckSymfonyDemo\Business\CommentBusiness;
use DuckSymfonyDemo\Business\MailBusiness;
Enter fullscreen mode Exit fullscreen mode

18 → 3. The missing 15 were Entity, Form, Repository, Event, Attribute, Cache — concepts DuckPHP simply does not have. The controller is left with one job: take input → call business → hand over to the view.

public function index()
{
    $page = (int) (Helper::Parameter('page') ?: 1);
    $tagName = $this->_tagName();
    $pager = BlogBusiness::_()->getIndex($page, $tagName);
    Helper::Show(['pager' => $pager, 'tagName' => $tagName], 'blog/index');
}
Enter fullscreen mode Exit fullscreen mode

The overall footprint is smaller too: src/ went from 34 files / 3424 lines to 27 files / 2777 lines; templates from 32 Twig files / 1164 lines to 19 PHP files / 648 lines.
There is exactly one call chain, and it is one-way: System → Controller → Business → Model. Model is the only layer that touches SQL, Business is stateless and never reads $_GET, and each layer uses only its own Helper.

That said, it was not all smooth.

Hard part one: the locale segment sits in the middle of the path

The Symfony Demo puts the language in the first URL segment (/{_locale}/blog/...), while DuckPHP's model is "each path segment is a controller/method" — it has no notion of a segment inserted in the middle. So I added RouteHookSymfony, the only class in the project that reaches into the core: it consumes "deploy prefix + locale segment" first, then dispatches.

The price: once the prefix is stripped up front, three framework components that assume the original path all get out of sync (Route strips the prefix, RouteMap only validates it, Resource rebuilds it). Measured: touch none of them and everything under /demo/ returns 404. So the component also has to compensate for all three copies inside its own initOptions() — and only when a deploy prefix exists. I wrote three screens of comments there, because without them a future reader will read it as a pile of hacks.

Hard part two: the whole site can move into a subdirectory

What added another layer was a second requirement: the app must run at the site root /, and be movable into /demo/ as a whole — without touching a line of business code.

On the DuckPHP side that is one option:

'controller_url_prefix' => 'demo/',   // flip this line and the site moves from / to /demo/
Enter fullscreen mode Exit fullscreen mode

Every link, static asset and 302 redirect picks up /demo automatically. Sounds cheap — but stacked with the locale segment above it becomes /demo/zh_CN/blog/...: both prefixes must be consumed by the same hook before routing, which is where the third piece of compensation (prefixes for static asset URLs) comes from.

In other words: "the deploy shape is configurable" is itself a cost. DuckPHP squeezes it down to one line of config, but not to zero.

Was it worth it? 40 A/B comparisons

I wrote a comparison script that hits both sites on the same machine and compares status codes case by case:

blog paginated   page/4 out of range   200  200  OK
blog_post        missing slug          404  404  OK
blog tag filter  ?tag=lorem            200  200  OK
logout           no token              403  403  OK
diff: 0 / 27
Enter fullscreen mode Exit fullscreen mode

Plus 8 authenticated checks: 17 posts in the admin list, 403 for someone else's post, 200 for the create/edit forms. The data is the original fixtures too: 30 posts, 150 comments, 9 tags.

The nicer part: embeddable, and overridable

There is one design I like a lot but never had a chance to mention: DuckPHP treats a project as a composable unit, not a closed application.

Which means two things:

  • projects can embed each other: a big system mounts a subsystem, each with its own namespace;
  • a project can be overridden without touching a single line of the child project: swap components, change options, replace hooks, replace views.

That is exactly how this version's route hook is written: it registers itself in ext, picks up the App options in its own initOptions() and compensates the three copies on the way, while the App layer only carries a declaration. In an earlier iteration I had jammed those 20 lines of compensation into App::onInited() and later moved them into the component — that is what "componentisation" actually feels like: change a component, and you do not have to patch the main flow.

Try it: mount this Demo as a sub-application of another DuckPHP app, then rewrite its homepage or admin a bit — without touching any file inside Duck/.


After finishing this version, my feeling comes down to one sentence:

Complexity is not because the business is complex; it is because the framework transfers the cost of its concepts onto the business code.

Which complexity is real, and which is just habit? That question deserves a serious answer, one day, from anyone maintaining a PHP project that keeps getting heavier.


Top comments (0)