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

Commit 39b94e7

Browse files
committed
👽 updated to match mvc core
1 parent 8419131 commit 39b94e7

14 files changed

Lines changed: 234 additions & 364 deletions

App/Controllers/Auth/AccountController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public function user()
1111
// The second parameter holds items to hide from the user array.
1212
$user = Auth::user("users", ["password"]);
1313

14-
if (!$user) throwErr(Auth::errors());
14+
if (!$user) response()->throwErr(Auth::errors());
1515

16-
json($user);
16+
response()->json($user);
1717
}
1818

1919
public function update()
@@ -48,8 +48,8 @@ public function update()
4848

4949
$user = Auth::update("users", $data, $where, $uniques);
5050

51-
if (!$user) throwErr(Auth::errors());
51+
if (!$user) response()->throwErr(Auth::errors());
5252

53-
json($user);
53+
response()->json($user);
5454
}
5555
}

App/Controllers/Auth/LoginController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class LoginController extends Controller
99
{
1010
public function index()
1111
{
12-
list($username, $password) = requestData(["username", "password"], true, true);
12+
list($username, $password) = request()->get(["username", "password"], true, true);
1313

1414
// You can now call leaf form methods statically.
1515
// Leaf v2.4.2 includes a new rule method which allows you to create
@@ -31,7 +31,7 @@ public function index()
3131
]);
3232

3333
// if validation fails, throw the errors
34-
if (!$validation) throwErr(Form::errors());
34+
if (!$validation) response()->throwErr(Form::errors());
3535

3636
// Simple logins with leaf auth. It takes in the table
3737
// to search for users in and the credentials to check
@@ -41,15 +41,15 @@ public function index()
4141
]);
4242

4343
// If user isn't found, show some errors
44-
if (!$user) throwErr(Auth::errors());
44+
if (!$user) response()->throwErr(Auth::errors());
4545

46-
json($user);
46+
response()->json($user);
4747
}
4848

4949
public function logout()
5050
{
5151
// If you use session with your tokens, you
5252
// might want to remove all the saved data here
53-
json("Logged out successfully!");
53+
response()->json("Logged out successfully!");
5454
}
5555
}

App/Controllers/Auth/RegisterController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ public function store()
1717
"password" => "min:8"
1818
]);
1919

20-
if (!$validation) throwErr(Form::errors());
20+
if (!$validation) response()->throwErr(Form::errors());
2121

2222
$user = Auth::register("users", $credentials, [
2323
"username", "email"
2424
]);
2525

26-
if (!$user) throwErr(Auth::errors());
26+
if (!$user) response()->throwErr(Auth::errors());
2727

28-
json($user);
28+
response()->json($user);
2929
}
3030
}

App/Controllers/UsersController.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
* This is a demo users controller put together to give
1212
* you an idea on basic features of leaf. Each block is commented
1313
* to help you understand exactly what's going on.
14-
*
14+
*
1515
* Some blocks can be used as alternatives depending on your preference,
1616
* you can switch to those as you see fit.
17-
*
17+
*
1818
* Although a demo, it's a real controller and works correctly as is.
1919
* You can continue your project like this or edit it to match your app.
2020
*/
@@ -34,8 +34,8 @@ public function login()
3434
// If you want to, you can perform some operation on the request object
3535
// $password = request()->get("password");
3636

37-
// You can also mass assign particular fields from the request
38-
list($username, $password) = requestData(["username", "password"], true, true);
37+
// You can also mass assign particular fields from the request
38+
list($username, $password) = request()->get(["username", "password"], true, true);
3939

4040
// You can perform operations on your model like this
4141
$user = User::where("username", $username)->first();
@@ -51,11 +51,11 @@ public function login()
5151
// password encoding has been configured in the base controller
5252

5353
// This line catches any errors that MAY happen
54-
if (!$user) throwErr($this->auth->errors());
54+
if (!$user) request()->throwErr($this->auth->errors());
5555

5656
// json is another global shortcut method
5757
// it's shorter than $this->json()
58-
json($user);
58+
response($user);
5959
}
6060

6161
public function register()
@@ -65,7 +65,7 @@ public function register()
6565
// $password = requestData("password");
6666

6767
// You can also directly pick vars from the request object
68-
$credentials = requestData(["username", "email", "password"]);
68+
$credentials = request(["username", "email", "password"]);
6969

7070
// You can validate your data with Leaf Form Validation
7171
$validation = $this->form->validate([
@@ -75,7 +75,7 @@ public function register()
7575
]);
7676

7777
// Throws an error if there's an issue in validation
78-
if (!$validation) throwErr($this->form->errors());
78+
if (!$validation) request()->throwErr($this->form->errors());
7979

8080
// Direct registration with Leaf Auth. Registers and initiates a
8181
// login, so you don't have to call login again, unless you want
@@ -86,17 +86,17 @@ public function register()
8686
]);
8787

8888
// throw an auth error if there's an issue
89-
if (!$user) throwErr($this->auth->errors());
89+
if (!$user) request()->throwErr($this->auth->errors());
9090

91-
json($user);
91+
response($user);
9292
}
9393

9494
public function recover_account()
9595
{
9696
$username = request("email");
9797

9898
$user = User::where("email", $username)->first() ?? null;
99-
if (!$user) throwErr(["email" => "Email not found"]);
99+
if (!$user) response()->throwErr(["email" => "Email not found"]);
100100

101101
// Set a temporary random password and reset user password
102102
$newPassword = rand(00000000, 99999999);
@@ -115,30 +115,30 @@ public function recover_account()
115115
"sender_name" => "API Name",
116116
]);
117117

118-
json(["message" => "ok"]);
118+
response()->json(["message" => "ok"]);
119119
}
120120

121121
public function reset_password()
122122
{
123123
// id retrieves the JWT from the headers, decodes it and returns
124124
// the user encoded into the token. If there's a problem with the token,
125125
// we can throw whatever error occurs. This means the user must be logged in.
126-
$userId = $this->auth->id() ?? throwErr($this->auth->errors());
126+
$userId = $this->auth->id() ?? response()->throwErr($this->auth->errors());
127127
$password = request("password");
128128

129-
// Get the
129+
// Get the
130130
$user = User::find($userId);
131-
if (!$user) throwErr(["user" => "User not found! Check somewhere..."]);
131+
if (!$user) response()->throwErr(["user" => "User not found! Check somewhere..."]);
132132

133133
// Change the user password
134134
$user->password = md5($password);
135135
$user->save();
136136

137137
// login again to get new token
138138
$user = $this->auth->login("users", ["id" => $userId]);
139-
if (!$user) throwErr($this->auth->errors());
139+
if (!$user) response()->throwErr($this->auth->errors());
140140

141-
json($user);
141+
response()->json($user);
142142
}
143143

144144
public function user() {
@@ -149,13 +149,13 @@ public function user() {
149149
// $auth->user() is new in v2.4 of leaf
150150
$user = $this->auth->user("users", $hidden);
151151

152-
json($user ?? throwErr($this->auth->errors()));
152+
response()->json($user ?? response()->throwErr($this->auth->errors()));
153153
}
154154

155155
public function edit()
156156
{
157157
// auth->id returns the user id encoded into jwt by default
158-
$userId = $this->auth->id() ?? throwErr($this->auth->errors());
158+
$userId = $this->auth->id() ?? response()->throwErr($this->auth->errors());
159159

160160
// data to update
161161
$data = request(["username", "email", "password"]);
@@ -168,6 +168,6 @@ public function edit()
168168

169169
$user = $this->auth->update("users", $data, $where, $uniques);
170170

171-
json($user ?? throwErr($this->auth->errors()));
171+
response()->json($user ?? response()->throwErr($this->auth->errors()));
172172
}
173173
}

App/Routes/_app.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/**@var Leaf\App $app */
44

55
$app->get("/", function () {
6-
json(["message" => "Congrats!! You're on Leaf API"], 200);
6+
response(["message" => "Congrats!! You're on Leaf API"]);
77
});
88

99
$app->get("/app", function () {
1010
// app() returns $app
11-
json(app()->routes(), 200);
11+
response(app()->routes());
1212
});

App/Routes/index.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
|
1212
*/
1313
$app->set404(function () {
14-
json("Resource not found", 404, true);
14+
response()->json("Resource not found", 404, true);
1515
});
1616

1717
/*
@@ -29,7 +29,7 @@
2929
}
3030
}
3131

32-
json("An error occured, our team has been notified", 500, true);
32+
response()->json("An error occured, our team has been notified", 500, true);
3333
});
3434

3535
/*

Config/app.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
| This tells leaf which directory to save and look for logs.
5151
|
5252
*/
53-
"log.dir" => storage_path("logs/"),
53+
"log.dir" => storagePath("logs/"),
5454

5555
/*
5656
|--------------------------------------------------------------------------
@@ -136,7 +136,7 @@
136136
| contains your Leaf application’s view files.
137137
|
138138
*/
139-
"views.path" => views_path(),
139+
"views.path" => ViewsPath(null, false),
140140

141141
/*
142142
|--------------------------------------------------------------------------
@@ -146,5 +146,5 @@
146146
| This config tells leaf where to save cached and compiled views.
147147
|
148148
*/
149-
"views.cachePath" => storage_path('framework/views'),
149+
"views.cachePath" => StoragePath('framework/views'),
150150
];

Config/auth.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,6 @@
110110
*/
111111
"USE_SESSION" => false,
112112

113-
/*
114-
|--------------------------------------------------------------------------
115-
| Turn off experimental warnings
116-
|--------------------------------------------------------------------------
117-
|
118-
| When experimental warnings are turned off, Leaf will ignore all features
119-
| which have been tagged as experimental. To actually get the feature to
120-
| work, follow the prompt in the warning.
121-
|
122-
*/
123-
"EXPERIMENTAL_WARNINGS" => false,
124-
125113
/*
126114
|--------------------------------------------------------------------------
127115
| Session on register

Config/bootstrap.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

Config/database.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Default Database Connection Name
8+
|--------------------------------------------------------------------------
9+
|
10+
| Here you may specify which of the database connections below you wish
11+
| to use as your default connection for all database work. Of course
12+
| you may use many connections at once using the Database library.
13+
|
14+
*/
15+
16+
"default" => _env("DB_CONNECTION", "mysql"),
17+
18+
/*
19+
|--------------------------------------------------------------------------
20+
| Database Connections
21+
|--------------------------------------------------------------------------
22+
|
23+
| Here are each of the database connections setup for your application.
24+
| Of course, examples of configuring each database platform that is
25+
| supported by eloquent is shown below to make development simple.
26+
|
27+
|
28+
| All database work in eloquent is done through the PHP PDO facilities
29+
| so make sure you have the driver for your particular database of
30+
| choice installed on your machine before you begin development.
31+
|
32+
*/
33+
34+
"connections" => [
35+
"sqlite" => [
36+
"driver" => "sqlite",
37+
"url" => _env("DATABASE_URL"),
38+
"database" => _env("DB_DATABASE", DatabasePath("database.sqlite")),
39+
"prefix" => "",
40+
"foreign_key_constraints" => _env("DB_FOREIGN_KEYS", true),
41+
],
42+
43+
"mysql" => [
44+
"driver" => "mysql",
45+
"url" => _env("DATABASE_URL"),
46+
"host" => _env("DB_HOST", "127.0.0.1"),
47+
"port" => _env("DB_PORT", "3306"),
48+
"database" => _env("DB_DATABASE", "forge"),
49+
"username" => _env("DB_USERNAME", "forge"),
50+
"password" => _env("DB_PASSWORD", ""),
51+
"unix_socket" => _env("DB_SOCKET", ""),
52+
"charset" => _env("DB_CHARSET", "utf8mb4"),
53+
"collation" => _env("DB_COLLATION", "utf8mb4_unicode_ci"),
54+
"prefix" => "",
55+
"prefix_indexes" => true,
56+
"strict" => true,
57+
"engine" => null,
58+
"options" => extension_loaded("pdo_mysql") ? array_filter([
59+
PDO::MYSQL_ATTR_SSL_CA => _env("MYSQL_ATTR_SSL_CA"),
60+
]) : [],
61+
],
62+
63+
"pgsql" => [
64+
"driver" => "pgsql",
65+
"url" => _env("DATABASE_URL"),
66+
"host" => _env("DB_HOST", "127.0.0.1"),
67+
"port" => _env("DB_PORT", "5432"),
68+
"database" => _env("DB_DATABASE", "forge"),
69+
"username" => _env("DB_USERNAME", "forge"),
70+
"password" => _env("DB_PASSWORD", ""),
71+
"charset" => _env("DB_CHARSET", "utf8"),
72+
"prefix" => "",
73+
"prefix_indexes" => true,
74+
"schema" => "public",
75+
"sslmode" => "prefer",
76+
],
77+
78+
"sqlsrv" => [
79+
"driver" => "sqlsrv",
80+
"url" => _env("DATABASE_URL"),
81+
"host" => _env("DB_HOST", "localhost"),
82+
"port" => _env("DB_PORT", "1433"),
83+
"database" => _env("DB_DATABASE", "forge"),
84+
"username" => _env("DB_USERNAME", "forge"),
85+
"password" => _env("DB_PASSWORD", ""),
86+
"charset" => _env("DB_CHARSET", "utf8"),
87+
"prefix" => "",
88+
"prefix_indexes" => true,
89+
],
90+
],
91+
];

0 commit comments

Comments
 (0)