MVC Globals ​
Leaf MVC comes with a couple of global functions that you can use to access your app's configuration, paths and more. These functions are available globally and can be used anywhere in your app.
Loading app paths ​
Since Leaf MVC comes with a robust structure out of the box, it also comes with quick ways to reference files in these structures. For example, if you want to reference a file in your public folder, you can use the PublicPath() helper.
AppPaths() ​
This returns an array of all the paths in your application.
$paths = AppPaths();
$controllersPath = AppPaths('controllers'); // you can do this
$controllersPath = $paths['controllers']; // or thisIf the path you are looking for doesn't have a helper function, you can use the AppPaths() helper to get the path. Just make sure that the path is defined in your config/paths.php file. If your Leaf MVC app does not come with a config/paths.php file, you can create one. This is the default structure of the config/paths.php file:
<?php
/*
|--------------------------------------------------------------------------
| Paths Config
|--------------------------------------------------------------------------
|
| Leaf allows you to completely modify the directory structure of your
| MVC application. This file just tells Leaf and other components
| where to find the important files in your app.
|
*/
return [
'myCustomPath' => 'app/myCustomPath',
'commands' => 'app/console',
'config' => 'config',
'channels' => 'app/channels',
'components' => 'app/components',
'controllers' => 'app/controllers',
'databaseStorage' => 'storage/app/db',
'events' => 'app/events',
'factories' => 'app/database/factories',
'helpers' => 'app/helpers',
'jobs' => 'app/jobs',
'lib' => 'lib',
'mail' => 'app/mail',
'middleware' => 'app/middleware',
'migrations' => 'app/database/migrations',
'models' => 'app/models',
'routes' => 'app/routes',
'schema' => 'app/database/schema',
'scripts' => 'app/scripts',
'seeds' => 'app/database/seeds',
'services' => 'app/services',
'storage' => 'storage',
'utils' => 'app/utils',
'views' => 'app/views',
'workers' => 'app/workers',
];After defining your paths in your config/paths.php file, you can use the AppPaths() helper to get the path.
AppPaths('myCustomPath');Keep in mind that not every path in your config/paths.php file has a helper function. If you want to get the path to a path that doesn't have a helper function, you can use the AppPaths() helper.
assets() ​
This returns the path to your assets folder. You can pass in a file name to get the path to that file.
$asset = assets('css/main.css');
// -> public/assets/css/main.cssYou can configure the path to your assets folder in your config/paths.php file.
'assets' => 'public/assets'ConfigPath() ​
This returns the path to your config folder. You can pass in a file name to get the path to that file.
$dbConfigFile = ConfigPath('db.php');
// -> config/db.phpCommandsPath() ​
This returns the path to your commands folder. You can pass in a file name to get the path to that file.
$command = CommandsPath('MainCommand.php');
// -> app/console/MainCommand.phpControllersPath() ​
This returns the path to your controllers folder. You can pass in a file name to get the path to that file.
$controller = ControllersPath('MainController.php');
// -> app/controllers/MainController.phpDatabasePath() ​
This returns the path to your database folder. You can pass in a file name to get the path to that file.
$database = DatabasePath('migrations');
// -> app/database/migrationsFactoriesPath() ​
This returns the path to your factories folder. You can pass in a file name to get the path to that file.
$factory = FactoriesPath('UserFactory.php');
// -> app/database/factories/UserFactory.phpHelpersPath() ​
This returns the path to your helpers folder. You can pass in a file name to get the path to that file.
$helper = HelpersPath('MainHelper.php');
// -> app/helpers/MainHelper.phpLibPath() ​
This returns the path to your lib folder. You can pass in a file name to get the path to that file.
$lib = LibPath('MainLib.php');
// -> lib/MainLib.phpMigrationsPath() ​
This returns the path to your migrations folder. You can pass in a file name to get the path to that file.
$migration = MigrationsPath('MainMigration.php');
// -> app/database/migrations/MainMigration.phpModelsPath() ​
This returns the path to your models folder. You can pass in a file name to get the path to that file.
$model = ModelsPath('User.php');
// -> app/models/User.phpPublicPath() ​
This returns the path to your public folder. You can pass in a file name to get the path to that file.
$public = PublicPath('index.php');
// -> public/index.phpRoutesPath() ​
This returns the path to your routes folder. You can pass in a file name to get the path to that file.
$routes = RoutesPath('_auth.php');
// -> app/routes/_auth.phpSeedsPath() ​
This returns the path to your seeds folder. You can pass in a file name to get the path to that file.
$seed = SeedsPath('MainSeed.php');
// -> app/database/seeds/MainSeed.phpStoragePath() ​
This returns the path to your storage folder. You can pass in a file name to get the path to that file.
$storage = StoragePath('MainStorage.php');
// -> storage/MainStorage.phpViewsPath() ​
This returns the path to your views folder. You can pass in a file name to get the path to that file.
$view = ViewsPath('index.leaf.php');
// -> app/views/index.leaf.phpLoading app config ​
There are some situations that may require you to load up your config files. For such situations, we've prepared a couple of helpers to help you load up your config files.
MvcConfig() ​
This returns an array of all the config files in your application.
$configs = MvcConfig();
$dbConfig = MvcConfig('db'); // you can do this
$dbConfig = $configs['db']; // or thisIt also allows you to load up a specific config from the config file you pass in.
$config = MvcConfig('db', 'host'); // you can do this
$config = $configs['db']['host']; // or thisAppConfig() ​
This returns an array of all the config in your config/app.php file.
$configs = AppConfig();
$debug = AppConfig('debug'); // you can do this
$debug = $configs['debug']; // or thisAuthConfig() ​
This returns an array of all the config in your config/auth.php file.
$configs = AuthConfig();
$auth = AuthConfig('auth'); // you can do this
$auth = $configs['auth']; // or thisCorsConfig() ​
This returns an array of all the config in your config/cors.php file.
$configs = CorsConfig();
$origin = CorsConfig('origin'); // you can do this
$origin = $configs['origin']; // or thisDatabaseConfig() ​
This returns an array of all the config in your config/db.php file.
$configs = DatabaseConfig();
$host = DatabaseConfig('host'); // you can do this
$host = $configs['host']; // or thisMailConfig() ​
This returns an array of all the config in your config/mail.php file.
$configs = MailConfig();
$host = MailConfig('host'); // you can do this
$host = $configs['host']; // or thisViewConfig() ​
This returns an array of all the config in your config/view.php file.
$configs = ViewConfig();
$host = ViewConfig('viewEngine'); // you can do this
$host = $configs['viewEngine']; // or thisView Helpers ​
View helpers are in charge of outputting your views.
view() ​
This method calls the render() method of whatever view engine you are using. It takes in the name of the view you want to render and an optional array of data you want to pass to the view.
view('index', [
'name' => 'Leaf'
]);The only issue here is that not all view engines directly output the view. For example, Blade and Twig return the view as a string. This means that you have to echo the view.
echo view('index', [
'name' => 'Leaf'
]);To make this easier, Leaf MVC ships with a render() method that calls the view() helper and echoes the view.
render() ​
This method calls the view() helper and echoes the view. It takes in the name of the view you want to render and an optional array of data you want to pass to the view.
render('index', [
'name' => 'Leaf'
]);