Skip to content

Commit bef1ce8

Browse files
committed
✨ Added view config
1 parent d762091 commit bef1ce8

File tree

5 files changed

+120
-52
lines changed

5 files changed

+120
-52
lines changed

App/Controllers/Controller.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ public function __construct()
3333
// You can configure auth to get additional customizations
3434
// This can be done here with the Auth::config method or
3535
// simply in the Config/auth.php file
36-
$this->auth->config(authConfig("settings"));
36+
$this->auth->config(authConfig());
3737

3838
// You can refer to https://leafphp.netlify.app/#/leaf/v/2.4/core/auth for auth docs
3939

40-
// New in v2.5. This alloows us to direct our attention
41-
// to session authentication instead of the default API JWT method.
40+
// New in v2.5. This allows us to use session
41+
// authentication instead of the default API JWT method.
4242
$this->auth->useSession();
4343
}
4444
}

App/Routes/index.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,10 @@
2424
*/
2525
$app->setNamespace("\App\Controllers");
2626

27-
2827
// $app is the instance of Leaf
29-
// $app->get("/", function () {
30-
// json(["message" => "Congrats!! You're on Leaf API"], 200);
31-
// });
3228
$app->view("/", "index");
3329

3430
$app->get("/app", function () {
35-
// app() returns $app
3631
json(app()->routes(), 200);
3732
});
3833

39-
// From v1.1, you can use this Route method anywhere in your app
40-
// This links to the login method of the UsersController
41-
// Route("POST", "/login", "UsersController@login");

Config/auth.php

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,45 @@
33
use Leaf\Helpers\Password;
44

55
return [
6-
"login" => "/auth/login",
7-
"register" => "/auth/register",
8-
"authenticated" => "/home",
9-
"logout" => "/auth/logout",
10-
"settings" => [
11-
"USE_TIMESTAMPS" => true,
12-
// Password encode is run when leaf wants to encode passwords on register
13-
// This exact method is used by default in Leaf, so you can delete it if
14-
// you want to.
15-
"PASSWORD_ENCODE" => function ($password) {
16-
return Password::hash($password);
17-
},
18-
// this function is run to verify the password. It's done by default,
19-
// so you can remove/set this and the above lines null if you wish to.
20-
"PASSWORD_VERIFY" => function ($password, $hashedPassword) {
21-
// Inside the password_verify method, you have access to the password and the hashed password
22-
return Password::verify($password, $hashedPassword);
23-
},
24-
"PASSWORD_KEY" => "password",
25-
"HIDE_ID" => true,
26-
"HIDE_PASSWORD" => true,
27-
"LOGIN_PARAMS_ERROR" => "Username not registered!",
28-
"LOGIN_PASSWORD_ERROR" => "Password is incorrect!",
29-
// USE_SESSION is experimental and may cause problems for your whole
30-
// authentication. If this happens, revert to the default auth and
31-
// manage sessions manually.
32-
"USE_SESSION" => false,
33-
// Create a session on registration?
34-
"SESSION_ON_REGISTER" => false,
35-
// Login page
36-
"GUARD_LOGIN" => "/auth/login",
37-
// Register page
38-
"GUARD_REGISTER" => "/auth/register",
39-
// Logout route
40-
"GUARD_LOGOUT" => "/auth/logout",
41-
// Home page
42-
"GUARD_HOME" => "/home",
43-
"SAVE_SESSION_JWT" => false,
44-
],
6+
// automatically add created_at/updated_at timestamps?
7+
"USE_TIMESTAMPS" => true,
8+
// Password encode is run when leaf wants to encode passwords on register
9+
// This exact method is used by default in Leaf, so you can delete it if
10+
// you want to.
11+
"PASSWORD_ENCODE" => function ($password) {
12+
return Password::hash($password);
13+
},
14+
// this function is run to verify the password. It's done by default,
15+
// so you can remove/set this and the above lines null if you wish to.
16+
"PASSWORD_VERIFY" => function ($password, $hashedPassword) {
17+
// Inside the password_verify method, you have access to the password and the hashed password
18+
return Password::verify($password, $hashedPassword);
19+
},
20+
// the default password key
21+
"PASSWORD_KEY" => "password",
22+
// Hide id field from user object?
23+
"HIDE_ID" => true,
24+
// Hide password field from user object?
25+
"HIDE_PASSWORD" => true,
26+
// Login params error
27+
"LOGIN_PARAMS_ERROR" => "Username not registered!",
28+
// Login password error
29+
"LOGIN_PASSWORD_ERROR" => "Password is incorrect!",
30+
// USE_SESSION is experimental!!!
31+
// If you encounter any problems using any new auth session features,
32+
// revert to the default auth and manage sessions manually.
33+
// Don't forget to open an issue.
34+
"USE_SESSION" => false,
35+
// Create a session on registration?
36+
"SESSION_ON_REGISTER" => false,
37+
// Login page
38+
"GUARD_LOGIN" => "/auth/login",
39+
// Register page
40+
"GUARD_REGISTER" => "/auth/register",
41+
// Logout route
42+
"GUARD_LOGOUT" => "/auth/logout",
43+
// Home page
44+
"GUARD_HOME" => "/home",
45+
// Add an auth token to the auth session?
46+
"SAVE_SESSION_JWT" => false,
4547
];

Config/functions.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ function plural($value, $count = 2)
110110
if (!function_exists('render')) {
111111
function render(string $view, array $data = [], array $mergeData = [])
112112
{
113-
markup(view($view, $data, $mergeData));
113+
if (viewConfig("view_engine") === \Leaf\Blade::class) {
114+
return markup(view($view, $data, $mergeData));
115+
}
116+
117+
return viewConfig("render")($view, $data);
114118
}
115119
}
116120

@@ -222,7 +226,7 @@ function throwErr($error, int $code = 500, bool $useMessage = false)
222226
*/
223227
function view(string $view, array $data = [], array $mergeData = [])
224228
{
225-
app()->blade->configure(views_path(), storage_path("framework/views"));
229+
app()->blade->configure(viewConfig("views_path"), viewConfig("cache_path"));
226230
return app()->blade->render($view, $data, $mergeData);
227231
}
228232
}
@@ -238,6 +242,17 @@ function authConfig($setting = null)
238242
return !$setting ? $config : $config[$setting];
239243
}
240244

245+
// Views
246+
247+
/**
248+
* Get view configuration
249+
*/
250+
function viewConfig($setting = null)
251+
{
252+
$config = require __DIR__ . "/view.php";
253+
return !$setting ? $config : $config[$setting];
254+
}
255+
241256
// App paths as callable methods
242257

243258
/**

Config/view.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
return [
4+
/*
5+
|--------------------------------------------------------------------------
6+
| View Storage Paths
7+
|--------------------------------------------------------------------------
8+
|
9+
| Most templating systems load templates from disk. Here you may specify
10+
| an array of paths that should be checked for your views. Of course
11+
| the usual LeafMVC view path has already been registered for you.
12+
|
13+
*/
14+
"views_path" => views_path(),
15+
16+
/*
17+
|--------------------------------------------------------------------------
18+
| Compiled View Path
19+
|--------------------------------------------------------------------------
20+
|
21+
| This option determines where all the compiled Blade templates will be
22+
| stored for your application. Typically, this is within the storage
23+
| directory. However, as usual, you are free to change this value.
24+
|
25+
*/
26+
"cache_path" => storage_path('framework/views'),
27+
28+
/*
29+
|--------------------------------------------------------------------------
30+
| Template Engine [EXPERIMENTAL]
31+
|--------------------------------------------------------------------------
32+
|
33+
| Leaf MVC unlike other frameworks tries to give you as much control as
34+
| you need. As such, you can decide which view engine to use.
35+
|
36+
*/
37+
"view_engine" => \Leaf\Blade::class,
38+
39+
/*
40+
|--------------------------------------------------------------------------
41+
| Custom render method [EXPERIMENTAL]
42+
|--------------------------------------------------------------------------
43+
|
44+
| This render method is triggered whenever render() is called
45+
| in your app if you're using a custom view engine.
46+
|
47+
*/
48+
"render" => function(string $view, array $data = []) {
49+
$veins = new \Leaf\Veins\Template;
50+
$veins->configure([
51+
"veins_dir" => views_path(null, false),
52+
"cache_dir" => storage_path('framework/views/'),
53+
]);
54+
$veins->set($data);
55+
$veins->render($view);
56+
57+
// This example is what veins would look like
58+
},
59+
];

0 commit comments

Comments
 (0)