I've seen similiar questions but haven't found a working solution. Here is my index.php file:
<?php
// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
$config = require(__DIR__ . '/../config/web.php');
(new yii\web\Application($config))->run();
require(__DIR__ . '/../config/aliases.php');
As you can see, it requires config/aliases.php file, whose contents are:
<?php
Yii::setAlias('message_files_root', '#webroot/message_files');
Yii::setAlias('message_files', '#web/message_files');
CallingYii::getAlias('#message_files_root') works fine within the index.php, but when it is in a controller file, it says Invalid path alias: #message_files_root.
Here is my controller's action (the rest of the code is fully commented):
public function actionIndex()
{
var_dump(Yii::getAlias('#message_files_root'));
$message = new Message();
if you invoke Yii::getAlias() static method from controller, Yii::setAlias(#web) does not get register yet.
you may refer to the web/index file.
web/index.php
require(__DIR__ . '/../../vendor/autoload.php');
require(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../../common/config/bootstrap.php'); --> Yii::setAlias(#web) are not listed here
require(__DIR__ . '/../config/bootstrap.php');
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/../../common/config/main.php'),
require(__DIR__ . '/../../common/config/main-local.php'),
require(__DIR__ . '/../config/main.php'),
require(__DIR__ . '/../config/main-local.php')
);
$application = new yii\web\Application($config);
//var_dump(Yii::getAlias('#web'));die; --> imitate invoke #web alias fail
$application->run(); --> here both #web alias and aliases from $config got registered.
//var_dump(Yii::getAlias('#web'));die; --> imitate invoke #web alias success
my guess is, the config got registered first before the default #web aliases being register. Thats why invoking #web aliases from bootrap config fail.
thus, u need to use absolute path for the second paramter. use this..
yii2 advanced template
Yii::setAlias('#message_files_root', dirname(dirname(__DIR__)) . '/frontend/web/message_files');
yii2 basic template
Yii::setAlias('#message_files_root', dirname(__DIR__) . '/web/message_files');
I am NOT claiming that is the nicest way to do that, but it works the way I wanted it to work – when an alias can include other aliases.
<?php
// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
$config = require(__DIR__ . '/../config/web.php');
$app = new class($config) extends yii\web\Application
{
protected function bootstrap()
{
parent::bootstrap();
require(__DIR__ . '/../config/aliases.php');
}
};
$app->run();
Related
Someone else has configured my computer and I don't know how. The script to run my application is not in web/index.php but public_html/index.php. I don't know if that makes a difference. The application uses a mysql database and a react front end and is hosted on aws. Here is the code from the public_html/index.php file:
<?php
require(__DIR__ . '/../vendor/autoload.php');
if (file_exists(__DIR__ . '/../.env')) {
(new \Dotenv\Dotenv(__DIR__ . '/../'))->load();
}
$yii_env = 'test';
switch (getenv('ENVIRONMENT')) {
case 'local': $yii_env = 'dev'; break;
case 'qa': $yii_env = 'test'; break;
case 'prod': $yii_env = 'prod'; break;
}
defined('YII_DEBUG') or define('YII_DEBUG', getenv('DEBUG'));
defined('YII_ENV') or define('YII_ENV', $yii_env);
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../common/config/bootstrap.php');
require(__DIR__ . '/../api/config/bootstrap.php');
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/../common/config/main.php'),
require(__DIR__ . '/../api/config/main.php')
);
$application = new yii\web\Application($config);
$application->run();
How do I access gii?
If you're using apache2. Please check to make sure you have ".htaccess" file in public_html/ folder.
I have created my own table on my wordpress database and created the filter search form. Now, my problem is that, I dont know how to connect my database to my search filter form in wordpress.
Here is the process, I send the keyword from index.php to result.php, and it shows nothing, just a blank page.
Here's my code:
<?php
define('WP_USE_THEMES', false);
global $wpdb;
$destination = $_POST["destination"];
echo "Welcome test" . $destination;
$sql = "SELECT * FROM rates WHERE location = '" . $destination ."';";
echo $sql;
echo "search this";
foreach ( $wpdb->get_results($sql) as $result ) {
echo "search for". $result->location;
}
?>
wp-content/themes/Your-Theme-Name/result.php
<?php
/*
* Template Name: MyResultPage
*/
/* You can also use require_once('wp-load.php'); */
get_header();
global $wpdb;
$destination = $_POST["destination"];
$table = $wpdb->prefix . "rates";
$sql = "SELECT * FROM `". $table . "` WHERE location = '". $destination. "'" ;
$result = $wpdb->get_results($sql);
foreach($result as $res) {
echo "search for". $res->location;
}
?>
Then, you create a new page in your wordpress admin and set the template to result.php template name: MyResultPage (for example).
you should either include wp-config.php file in the header of the file which will include all the relevant files in your page.
Can you plz check below code and it output.
i added one page in root directory of WordPress and make one page and add this code.
test.php
<?php
define('WP_USE_THEMES', false);
require_once('wp-load.php');
global $wpdb;
$result = $wpdb->get_results(("SELECT * FROM {$wpdb->prefix}posts WHERE post_type = 'page'"), ARRAY_A);
foreach( $result as $key => $val )
{
echo '<p>Title - '.$val['post_name'].'</p>';
echo '<p>ID - '.$val['ID'].'</p>';
}
?>
Now here is out put of this page it shows all page title and id.
Output:
<p>Title - sample-page</p>
<p>ID - 2</p>
<p>Title - sample-page1</p>
<p>ID - 3</p>
How to integrate citruspay payment gateway in yii2? Citruspay provide library for payment integration. When i am using that library it gives error "Class 'seller\citruspay\lib\Zend_Config_Ini' not found" - any in this will be appreciated
PHP Fatal Error – yii\base\ErrorException
Class 'seller\modules\citruspay\controllers\CitrusPay' not found
1. in C:\wamp\www\haggl\seller\modules\citruspay\controllers\DefaultController.php at line 44
35363738394041424344454647484950515253
"cardHolderName" => Yii::$app->request->post('cardHolderName'),
"cardNumber" => Yii::$app->request->post('cardNumber'),
"expiryMonth" => Yii::$app->request->post('expiryMonth'),
"cardType" => Yii::$app->request->post('cardType'),
"cvvNumber" => Yii::$app->request->post('cvvNumber'),
"expiryYear" => Yii::$app->request->post('expiryYear'),
"returnUrl" => Yii::$app->request->post('returnUrl'),
"amount" => Yii::$app->request->post('amount'),
);
CitrusPay::setApiKey("apikey",'sandbox');
$response = Transaction::create($tarr,CitrusPay::getApiKey());
$redirectUrl = $response->get_redirect_url();
$response_code = $response->get_resp_code();
if($redirectUrl != "" && $response_code == 200)
{
return $this->redirect($redirectUrl);
}else{
$html = "<p>Response Code is ".$response->get_resp_code()."</p>";
$html .= "<p>Response Message is ".$response->get_resp_msg()."</p>";
2. in C:\wamp\www\haggl\seller\vendor\yiisoft\yii2\base\InlineAction.php – call_user_func_array:{C:\wamp\www\haggl\seller\vendor\yiisoft\yii2\base\InlineAction.php:55}() at line 55
3. in C:\wamp\www\haggl\seller\vendor\yiisoft\yii2\base\Controller.php – yii\base\InlineAction::runWithParams() at line 151
4. in C:\wamp\www\haggl\seller\vendor\yiisoft\yii2\base\Module.php – yii\base\Controller::runAction() at line 455
5. in C:\wamp\www\haggl\seller\vendor\yiisoft\yii2\web\Application.php – yii\base\Module::runAction() at line 83
6. in C:\wamp\www\haggl\seller\vendor\yiisoft\yii2\base\Application.php – yii\web\Application::handleRequest() at line 375
7. in C:\wamp\www\haggl\seller\index.php – yii\base\Application::run() at line 22
16171819202122
require(__DIR__ . '/../common/config/main-local.php'),
require(__DIR__ . '/config/main.php'),
require(__DIR__ . '/config/main-local.php')
);
$application = new yii\web\Application($config);
$application->run();
Solution:
In "index.php" file which is present in Yii2 folder make changes as given below,
<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
// register CitrusPay path to yii enviornment
set_include_path(realpath(dirname(__FILE__)) . '/components/CitrusPay/lib'.PATH_SEPARATOR.get_include_path());
// register CitrusPay autoloader
require(realpath(dirname(__FILE__)) . '/components/CitrusPay/lib/CitrusPay.php');
// register Composer autoloader
require(__DIR__ . '/vendor/autoload.php');
// include Yii class file
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');
Yii::$classMap['CitrusPay'] = (realpath(dirname(__FILE__)) . '/components/CitrusPay/lib/CitrusPay.php');
// load application aliases
require(__DIR__ . '/../common/config/aliases.php');
// load application configuration
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/../common/config/main.php'),
require(__DIR__ . '/../common/config/main-local.php'),
require(__DIR__ . '/config/main.php'),
require(__DIR__ . '/config/main-local.php')
);
$application = new yii\web\Application($config);
$application->run();
And then call the library classes using the "use" keyword
For Example:
use CitrusPay;
And it will work like a charm..
I have a console command to generate user report. I want to call the same from my web application. I am using Yii 2.0.0 beta version.I tried to follow answers given in this post How to call a console command in web application action in Yii?
Since Yii 2.0 structure is very different from Yii 1.1 ,I get errors if I try to include command/userReportController.php .Can someone guide me on this?
You should use an extension like https://github.com/vova07/yii2-console-runner-extension
I think this is the simplest solution:
$controller = new YourConsoleController(Yii::$app->controller->id, Yii::$app);
$controller->actionYourConsoleAction();
use this code:
$application = new yii\console\Application($config);
$application->runAction('controller/action');
I'm using this method instead of yii console command, because I'm running Yii on managed VPS where unix commands are not supported in cron, only php scripts.
To run it this way instead of console, the yii configuration must be initialized first, of course:
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/vendor/autoload.php');
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/common/config/bootstrap.php');
require(__DIR__ . '/console/config/bootstrap.php');
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/common/config/main.php'),
require(__DIR__ . '/common/config/main-local.php'),
require(__DIR__ . '/console/config/main.php'),
require(__DIR__ . '/console/config/main-local.php')
);
Let's say your action is called file/convert, and you want to run it in the background:
$cmd = "cd /home/site.com/public_html/yii && nohup php yii file/convert >/dev/null &";
shell_exec( $cmd );
yii2-console-runner-extension keeps loading.
Try this:
It's yii (without ext, not yii.bat)
Make sure that php folder added to PATH variable (Windows)
$op = shell_exec ( 'absolute/path/to/yii ' . 'cache/flush-all' );
\yii\helpers\VarDumper::dump($op, 10, 1);
Output:
The following cache components were processed:
* cache (yii\\caching\\FileCache)
If you really want to run Console controller by Web controller likes migrate:
public function actionMigrate()
{
// Keep current application
$oldApp = \Yii::$app;
// Load Console Application config
$config = require \Yii::getAlias('#app'). '/config/console.php';
new \yii\console\Application($config);
$result = \Yii::$app->runAction('migrate', ['migrationPath' => '#app/migrations/', 'interactive' => false]);
// Revert application
\Yii::$app = $oldApp;
return;
}
Above sample code is for yii2-app-basic template, you could change path for yii2-app-advanced template.
I am using dotProject (OpenSource project management tool) and need do some custom sql selections for my own purposes.
select u.user_username,
p.project_name,
p.project_start_date,
p.project_end_date,
SUBSTRING(sysval_value,LOCATE(CONCAT(p.project_status,'|'),sysval_value)+2,LOCATE('\n',sysval_value,LOCATE(CONCAT(p.project_status,'|'),sysval_value))-LOCATE(CONCAT(p.project_status,'|'),sysval_value)-2) as project_status
from project_contacts pc
left join users u on pc.contact_id = u.user_id
left join sysvals s on s.sysval_title='ProjectStatus'
left join projects p on p.project_id=pc.project_id
notice the nasty way I had to select the ProjectStatus from the sysvals table.
The sysval_value for ProjectStatus looks like the following:
0|Not Defined/n1|Proposed/n2|In Planning/n3|In Progress/n4|On Hold/n5|Complete/n6|Template/n7|Archived
Not sure why it is like this, I would have expected a status table with a primary key and a description (maybe just to eliminate the number of tables in the project).
Is there a better way of selecting from this sort of delimited string in MySQL?
These sysval_values can be taken as an array with usingdPgetSysVal() method:
this method defined in dotProject->includes -> main_functions.php
$arr = array();
$arr=dPgetSysVal("ProjectStatus");
$arr[0]----------> Not Defined
$arr[1]----------> Proposed
$arr[2]----------> In Planning
$arr[3]----------> In Progress
$arr[4]----------> On Hold
$arr[5]----------> Complete and so on...
Here what i tried with your code for selection:
<?php
require_once 'base.php';
require_once DP_BASE_DIR . '/includes/config.php';
require_once (DP_BASE_DIR . '/classes/csscolor.class.php'); // Required before main_functions
require_once (DP_BASE_DIR . '/classes/kses.class.php'); // Required before main_functions
require_once (DP_BASE_DIR . '/includes/main_functions.php');
require_once (DP_BASE_DIR . '/includes/db_adodb.php');
require_once (DP_BASE_DIR . '/includes/db_connect.php');
require_once (DP_BASE_DIR . '/classes/ui.class.php');
require_once (DP_BASE_DIR . '/classes/permissions.class.php');
require_once (DP_BASE_DIR . '/includes/session.php');
require_once (DP_BASE_DIR . '/includes/permissions.php');
require_once (DP_BASE_DIR . '/style/dp-grey-theme/overrides.php');
$q = new DBQuery;
$q->clear();
$q->addTable('project_contacts');
$q->addTable('users');
$q->addTable('projects');
$q->addTable('sysvals');
$q->addWhere('dotp_project_contacts.contact_id = dotp_users.user_id');
$q->addWhere('dotp_sysvals.sysval_title = "ProjectStatus"');
$q->addWhere('dotp_projects.project_id = dotp_project_contacts.project_id');
$values = $q->loadList();
$arr = array();
foreach ($values as $row) {
$arr=dPgetSysVal("ProjectStatus");
echo $row['user_username'].' '. $row['project_name'].' '. $row['project_start_date'].' '. $row['project_end_date'].' '. $arr[$row['project_status']].'<br>';
}
?>
dotp_ my db table extension :)