@@ -264,3 +264,150 @@ public function serializeIndex($request, $serialized)
264264}
265265```
266266
267+ ## Custom routes
268+
269+ Laravel Restify has its own "CRUD" routes, however you're able to define your own routes right from your Repository class:
270+
271+ ``` php
272+ /**
273+ * Defining custom routes
274+ *
275+ * The default prefix of this route is the uriKey (e.g. 'restify-api/posts'),
276+ *
277+ * The default namespace is AppNamespace/Http/Controllers
278+ *
279+ * The default middlewares are the same from config('restify.middleware')
280+ *
281+ * However all options could be overrided by passing an $options argument
282+ *
283+ * @param \Illuminate\Routing\Router $router
284+ * @param $options
285+ */
286+ public static function routes(\Illuminate\Routing\Router $router, $options = [])
287+ {
288+ $router->get('hello-world', function () {
289+ return 'Hello World';
290+ });
291+ }
292+ ```
293+
294+ Let's diving into a more "real life" example. Let's take the Post repository we had above:
295+
296+ ``` php
297+ use Illuminate\Routing\Router;
298+ use Binaryk\LaravelRestify\Repositories\Repository;
299+
300+ class Post extends Repository
301+ {
302+ /*
303+ * @param \Illuminate\Routing\Router $router
304+ * @param $options
305+ */
306+ public static function routes(Router $router, $options = [])
307+ {
308+ $router->get('/{id}/kpi', 'PostController@kpi');
309+ }
310+
311+ public static function uriKey()
312+ {
313+ return 'posts';
314+ }
315+ }
316+ ```
317+
318+ At this moment Restify built the new route as a child of the ` posts ` , so it has the route:
319+
320+ ``` http request
321+ GET: /restify-api/posts/{id}/kpi
322+ ```
323+
324+ This route is pointing to the ` PostsController ` , let's define it:
325+
326+ ``` php
327+ <?php
328+
329+ namespace App\Http\Controllers;
330+
331+ use Illuminate\Http\JsonResponse;
332+ use Binaryk\LaravelRestify\Controllers\RestController;
333+
334+ class PostController extends RestController
335+ {
336+ /**
337+ * Show the profile for the given user.
338+ *
339+ * @param int $id
340+ * @return JsonResponse
341+ */
342+ public function kpi($id)
343+ {
344+ //...
345+
346+ return $this->response();
347+ }
348+ }
349+ ```
350+
351+ ### Custom prefix
352+
353+ As we noticed in the example above, the route is generated as a child of the current repository ` uriKey ` route,
354+ however sometimes you may want to have a separate prefix, which doesn't depends of the URI of the current repository.
355+ Restify provide you an easy of doing that, by adding default value ` prefix ` for the second ` $options ` argument:
356+
357+ ``` php
358+ /**
359+ * @param \Illuminate\Routing\Router $router
360+ * @param $options
361+ */
362+ public static function routes(Router $router, $options = ['prefix' => 'api',])
363+ {
364+ $router->get('hello-world', function () {
365+ return 'Hello World';
366+ });
367+ }
368+ ````
369+
370+ Now the generated route will look like this:
371+
372+ ```http request
373+ GET: '/api/hello-world
374+ ```
375+
376+ With ` api ` as a custom prefix.
377+
378+
379+ ### Custom middleware
380+
381+ All routes declared in the ` routes ` method, will have the same middelwares defined in your ` restify.middleware ` configuration file.
382+ Overriding default middlewares is a breeze with Restify:
383+
384+ ``` php
385+ /**
386+ * @param \Illuminate\Routing\Router $router
387+ * @param $options
388+ */
389+ public static function routes(Router $router, $options = ['middleware' => [CustomMiddleware::class],])
390+ {
391+ $router->get('hello-world', function () {
392+ return 'Hello World';
393+ });
394+ }
395+ ````
396+
397+ In that case, the single middleware of the route will be defined by the `CustomMiddleware` class.
398+
399+ ### Custom Namespace
400+
401+ By default each route defined in the `routes` method, will have the namespace `AppRootNamespace\Http\Controllers`.
402+ You can override it easily by using `namespace` configuration key:
403+
404+ ```php
405+ /**
406+ * @param \Illuminate\Routing\Router $router
407+ * @param $options
408+ */
409+ public static function routes(Router $router, $options = ['namespace' => 'App\Services',])
410+ {
411+ $router->get('hello-world', 'WorldController@hello');
412+ }
413+ ````
0 commit comments