@@ -208,6 +208,157 @@ public function profile(Request $request): string
208208}
209209` ` `
210210
211+ # ## URL Helpers
212+
213+ The framework provides Rails-style URL helpers for generating URLs from named routes. This makes it easy to generate consistent URLs throughout your application.
214+
215+ # ### Route Naming
216+
217+ Routes are automatically named based on their configuration key in the YAML file :
218+
219+ ` ` ` yaml
220+ routes:
221+ user_profile: # This becomes the route name
222+ route: /users/{id}
223+ method: GET
224+ controller: App\C ontrollers\UserContr oller@profile
225+
226+ admin_user_posts:
227+ route: /admin/users/{user_id}/posts/{post_id}
228+ method: GET
229+ controller: App\C ontrollers\A dminController@userPosts
230+ ` ` `
231+
232+ # ### Using URL Helpers in Controllers
233+
234+ Controllers can use URL helpers directly via magic methods :
235+
236+ ` ` ` php
237+ class UserController extends Base
238+ {
239+ public function show($id): string
240+ {
241+ $user = User::find($id);
242+
243+ // Magic methods for URL generation
244+ $editUrl = $this->userEditPath(['id' => $id]);
245+ $absoluteUrl = $this->userProfileUrl(['id' => $id]);
246+
247+ // Use in redirects
248+ if (!$user) {
249+ return redirect($this->userIndexPath());
250+ }
251+
252+ return $this->renderHtml(HttpResponseStatus::OK, [
253+ 'user' => $user,
254+ 'edit_url' => $editUrl
255+ ]);
256+ }
257+
258+ public function create(): string
259+ {
260+ // After creating user, redirect using magic method
261+ $user = new User($request->all());
262+ $user->save();
263+
264+ return redirect($this->userProfilePath(['id' => $user->id]));
265+ }
266+ }
267+ ` ` `
268+
269+ # ### Direct URL Helper Methods
270+
271+ Controllers also provide direct helper methods :
272+
273+ ` ` ` php
274+ // Generate relative URLs
275+ $profileUrl = $this->urlFor('user_profile', ['id' => 123]);
276+
277+ // Generate absolute URLs
278+ $absoluteUrl = $this->urlForAbsolute('user_profile', ['id' => 123]);
279+
280+ // Check if route exists
281+ if ($this->routeExists('user_profile')) {
282+ // Route is available
283+ }
284+
285+ // Get UrlHelper instance for advanced usage
286+ $urlHelper = $this->urlHelper();
287+ ` ` `
288+
289+ # ### Using URL Helpers in Views
290+
291+ URL helpers are automatically available in all views through the injected `$urlHelper` variable :
292+
293+ ` ` ` php
294+ <!-- resources/views/user/profile.php -->
295+ <div class="user-profile">
296+ <h1><?= $user->name ?></h1>
297+
298+ <!-- Magic methods in views -->
299+ <a href="<?= $urlHelper->userEditPath(['id' => $user->id]) ?>" class="btn">Edit</a>
300+ <a href="<?= $urlHelper->userPostsPath(['user_id' => $user->id]) ?>" class="btn">View Posts</a>
301+
302+ <!-- Complex routes work too -->
303+ <a href="<?= $urlHelper->adminUserReportsPath(['id' => $user->id, 'year' => date('Y')]) ?>">
304+ Admin Reports
305+ </a>
306+
307+ <!-- Direct method calls -->
308+ <a href="<?= $urlHelper->routePath('user_profile', ['id' => $user->id]) ?>">Profile</a>
309+ <a href="<?= $urlHelper->routeUrl('user_profile', ['id' => $user->id]) ?>">Share Link</a>
310+ </div>
311+ ` ` `
312+
313+ # ### Magic Method Conventions
314+
315+ The magic methods follow Rails naming conventions :
316+
317+ | Route Name in YAML | Magic Method (Relative) | Magic Method (Absolute) | Generated URL |
318+ |---------------------|------------------------|-------------------------|---------------|
319+ | `user_profile` | `userProfilePath()` | `userProfileUrl()` | `/users/123` |
320+ | `user_edit` | `userEditPath()` | `userEditUrl()` | `/users/123/edit` |
321+ | `admin_user_posts` | `adminUserPostsPath()` | `adminUserPostsUrl()` | `/admin/users/1/posts/2` |
322+ | `blog_category` | `blogCategoryPath()` | `blogCategoryUrl()` | `/blog/category/tech` |
323+
324+ # ### URL Helper Methods
325+
326+ | Method | Description | Example |
327+ |--------|-------------|---------|
328+ | `routePath($name, $params)` | Generate relative URL | `$urlHelper->routePath('user_profile', ['id' => 123])` |
329+ | `routeUrl($name, $params)` | Generate absolute URL | `$urlHelper->routeUrl('user_profile', ['id' => 123])` |
330+ | `routeExists($name)` | Check if route exists | `$urlHelper->routeExists('user_profile')` |
331+ | `getAvailableRoutes()` | List all named routes | `$urlHelper->getAvailableRoutes()` |
332+ | `{routeName}Path($params)` | Magic method for relative URL | `$urlHelper->userProfilePath(['id' => 123])` |
333+ | `{routeName}Url($params)` | Magic method for absolute URL | `$urlHelper->userProfileUrl(['id' => 123])` |
334+
335+ # ### Error Handling
336+
337+ URL helpers gracefully handle missing routes :
338+
339+ ` ` ` php
340+ // Returns null if route doesn't exist
341+ $url = $urlHelper->nonExistentRoutePath(['id' => 123]);
342+
343+ if ($url === null) {
344+ // Handle missing route
345+ $url = $urlHelper->userIndexPath(); // fallback
346+ }
347+ ` ` `
348+
349+ # ### Advanced Usage
350+
351+ ` ` ` php
352+ // Get all available routes for debugging
353+ $routes = $urlHelper->getAvailableRoutes();
354+ foreach ($routes as $route) {
355+ echo "Route: {$route['name']} -> {$route['method']} {$route['path']}\n ";
356+ }
357+
358+ // Custom UrlHelper instance
359+ $customHelper = new UrlHelper($customRouter);
360+ ` ` `
361+
211362# # Configuration
212363
213364All YAML config file parameters can be overridden by environment variables in the form of `<CATEGORY>_<KEY>`, e.g.
@@ -562,17 +713,18 @@ neuron mvc:routes:list --json
562713
563714```
564715MVC Routes
565- ================================================================================
566- Pattern | Method | Controller | Action | Parameters
567- --------------------------------------------------------------------------------
568- / | GET | HomeController | index | -
569- /user/{id} | GET | UserController | profile | id
570- /api/users | GET | Api\UserController | list | -
571- /api/users | POST | Api\UserController | create | name, email
572- /products | GET | ProductController | list | -
573- /products/{id} | GET | ProductController | details | id
716+ ======================================================================================
717+ Name | Pattern | Method | Controller | Action
718+ --------------------------------------------------------------------------------------
719+ home | / | GET | HomeController | index
720+ user_profile | /user/{id} | GET | UserController | profile
721+ api_users_list | /api/users | GET | Api\UserController | list
722+ api_users_create | /api/users | POST | Api\UserController | create
723+ products_list | /products | GET | ProductController | list
724+ product_details | /products/{id} | GET | ProductController | details
574725
575726Total routes: 6
727+ Named routes: 6
576728Methods: GET: 4, POST: 2
577729```
578730
0 commit comments