Why Controllers in PageBlocks Are Needed — And Why They're Better Than the Classic MODX Approach
Classic MODX: Flexible but Chaotic
MODX offers many ways to handle logic: snippets, chunks, plugins, custom files in /assets/components/, TV variables, and even inline code. This provides flexibility but creates chaos:
- Logic is scattered across different files and contexts.
- Lots of manual code, especially for AJAX.
- Hard to reuse and scale.
- No unified architecture—everyone does things their own way.
PageBlocks — A Universal Approach
PageBlocks doesn’t break MODX’s standard workflow—you can still use Fenom, chunks, and snippets. But if your project grows, or you want to write clean, modern code, controllers are the best way.
With controllers, you get:
- Clear architecture.
- A unified style for both API and pages.
- Scalability.
- Easier maintenance.
Advantages of Controllers in PageBlocks
1. Clean Logic — No Mess in Plugins or Chunks
Instead of scattering business logic across plugins, chunks, or snippets, you just create a controller:
return response()->json(['success' => true]);Everything is structured, readable, and human-friendly.
2. Laravel-like Routing
You explicitly define paths and controllers:
Route::get('/api/products', 'ProductController@index');In MODX without PageBlocks, you’d have to create a separate file, manually load MODX, handle $_GET, $_POST, and write output manually.
3. REST Support and Any HTTP Methods
Controllers easily separate logic by methods:
Route::post('/feedback', 'FeedbackController@store');—for POST requests, without any hacks involving AJAX and chunks.
4. Built-in JSON, HTML, Redirects, and File Downloads
The controller decides what to return:
return $data;return view('product', $data);return redirect('/thanks');
No need to manually set headers or use third-party snippets.
5. Middleware and Route-Level Filtering
You can add access checks:
Route::get('/admin', 'AdminController@index')->middleware('auth');No need to create a plugin and manually check permissions in every snippet.
6. Controller Logic in PHP, Not Fenom or TVs
Everything is written in plain PHP, like in any modern framework. No need for Fenom tricks, nested chunks, [[$chunkName]], or other MODX old-school approaches.
7. Scalability
Controllers are easy to group, extend, and move into services—whether you prefer MVC or API. Support for prefix() and group() simplifies project structure.
8. Easier Testing and Debugging
You can test controllers independently of the MODX environment, and the logic isn’t hidden inside chunks or context.
9. Same Approach for Pages and AJAX
No difference between handling a regular page (/contacts) and an AJAX request (/api/send-form). One style, one architecture.
10. Fewer Files, Less Chaos
Instead of /assets/components/foo/ajax.php, /core/components/foo/elements/snippets/snippet.form.php, and countless includes—you just create FormController.php and define a route.
Real-World Examples
AJAX Product Filter Loading
MODX:
- Separate PHP file
ajax.products.php. - Manually load MODX.
- Write SQL or a snippet.
- Output JSON with headers.
PageBlocks:
Route::get('/api/products', 'ProductController@index');public function index()
{
$products = $this->queryProducts();
return $this->json($products);
}Protected /admin Section
MODX:
- Create a resource, set "admin-only access."
- Manually check permissions in chunks.
PageBlocks:
Route::get('/admin', 'AdminController@index')->middleware('auth');Conclusion
Classic MODX still works—and you can keep using it.
But if you want order, scalability, and enjoyable coding, controllers in PageBlocks take things to the next level.
You’re not dealing with snippet chaos but with a clear architecture, where every route leads to a logical controller—not a "black box."
PageBlocks gives you a choice.
Controllers make that choice powerful.