Skip to content
This repository was archived by the owner on Oct 8, 2024. It is now read-only.

Commit dd57c4e

Browse files
committed
🎨 organized example routes into modules
1 parent c887f45 commit dd57c4e

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

App/Routes/_app.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
/**@var Leaf\App $app */
4+
5+
$app->get("/", function () {
6+
json(["message" => "Congrats!! You're on Leaf API"], 200);
7+
});
8+
9+
$app->get("/app", function () {
10+
// app() returns $app
11+
json(app()->routes(), 200);
12+
});

App/Routes/index.php

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
/**@var Leaf\App $app */
4+
35
/*
46
|--------------------------------------------------------------------------
57
| Set up 404 handler
@@ -12,6 +14,24 @@
1214
json("Resource not found", 404, true);
1315
});
1416

17+
/*
18+
|--------------------------------------------------------------------------
19+
| Set up 500 handler
20+
|--------------------------------------------------------------------------
21+
|
22+
| Create a handler for error 500
23+
|
24+
*/
25+
$app->setErrorHandler(function ($e = null) use($app) {
26+
if ($e) {
27+
if ($app->config("log.enabled")) {
28+
$app->logger()->error($e);
29+
}
30+
}
31+
32+
json("An error occured, our team has been notified", 500, true);
33+
});
34+
1535
/*
1636
|--------------------------------------------------------------------------
1737
| Set up Controller namespace
@@ -23,25 +43,6 @@
2343
*/
2444
$app->setNamespace("\App\Controllers");
2545

26-
27-
// $app is the instance of Leaf
28-
$app->get("/", function () {
29-
json(["message" => "Congrats!! You're on Leaf API"], 200);
30-
});
31-
32-
$app->get("/app", function () {
33-
// app() returns $app
34-
json(app()->routes(), 200);
35-
});
36-
37-
// From v1.1, you can use this Route method anywhere in your app
38-
// This links to the login method of the UsersController
39-
// Route("POST", "/login", "UsersController@login");
40-
41-
// You can define your routes here directly or
42-
// import an independent route file
43-
44-
// Example authentication has been created for you to give you
45-
// an idea on working with this version of leaf. To get rid of all
46-
// the comments, simply run php leaf scaffold:auth --api
47-
require __DIR__ . "/_auth.php";
46+
// You can break up routes into individual files
47+
require __DIR__ . "/_app.php";
48+
require __DIR__ . "/_auth.php";

0 commit comments

Comments
 (0)