Create custom params file in yii framework
Create custom params in yii framework
Yii Framework comes with powerful inbuilt features special create custom params in yii framework eg.
- Model-View-Controller (MVC) design pattern
- Database Access Objects (DAO)
- Custom Query Builder, Active Record (AR) and DB Migration
- Form input and validation
- AJAX-enabled widgets
- Authentication and authorization
- Layout Skinning and theming
- Web services
- Internationalization (I18N) and localization (L10N)
- Layered caching scheme
- Error handling and logging
- Security
- Unit and functionality testing
- Automatic code generation (Gii Module)
- Friendly with third-party code
- Detailed documentation and Active Community
- Extension library
- Widget-Based Development
- Event-Driven Programming
- Scaffolding
- Multi-Tier Caching
- Dependency Injection Container
- Console Applications
- Lazy Loading and Eager Loading
- Web Services and SOAP Support
- Debugging Tools: Yii’s debug toolbar provides real-time insights into application performance, database queries, and memory usage during development.
- Queue Support: Yii includes built-in support for queuing systems, enabling background job processing using drivers like RabbitMQ, Redis, and Beanstalk.
- Cloud Integration: Yii integrates easily with cloud services like AWS, Azure, and Google Cloud for storage, computing, and APIs.
- Asset Management: Yii manages CSS, JavaScript, and other assets efficiently, supporting features like bundling and minimizing to improve performance.
- Dynamic Querying: Yii allows dynamic query construction with its query builder, supporting complex joins, conditions, and relations in a readable format.
Most of the Yii beginner use main.php file to create custom params in yii framework because that’s pattern comes in installed Yii framework. This is fine if you have less parameter but if you are building large web portal or application then it’s very hard to manage everything in mail.php because of its main config file. So the best way is creating separate params.php in the same directory and includes it in your main.php file. The check is the example of params.php and main.php files for create custom params in yii framework.
config/main.php :
'params'=>require(dirname(__FILE__).'/params.php'),
config/params.php :
<?php
return array(
'defaultPageSize'=>10,
'websiteName'=>'ezeelive.com',
'copyrightInfo'=>'Copyright © '.date("Y").' Ezeelive Technologies. All Rights Reserved.',
...
...
);
?>
In Yii2 Framework:
1. config/main.php (advance backend application):
<?php // You can add your custom params file here $params = array_merge( require(__DIR__ . '/../../common/config/params.php'), require(__DIR__ . '/../../common/config/params-local.php'), require(__DIR__ . '/params.php'), require(__DIR__ . '/params-local.php') ); return [ 'id' => 'backend-app',
'name' => 'Backend App',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'backend\controllers',
'modules' => [],
'components' => [
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
],
'params' => $params,
];
In this example, we are using params.php file.
2. In params.php:
<?php return [ 'adminEmail' => 'info@ezeelive.com',
];
3. Access param variable:
$params['mandrill_api_key']
OR
\Yii::$app->params['mandrill_api_key']
Leave a Reply