Yii2 got Error Message - yii2

anyone who can help me to fix this problem.....i got this error after i update my composer at my yii2 application..
also i got error messsage this:
* #return array the path directory and the URL that the asset is published as.
* #throws InvalidParamException if the asset to be published does not exist.
*/
protected function publishDirectory($src, $options)
{
$dir = $this->hash($src);
$dstDir = $this->basePath . DIRECTORY_SEPARATOR . $dir;
if ($this->linkAssets) {
if (!is_dir($dstDir)) {
symlink($src, $dstDir);
}
} elseif (!empty($options['forceCopy']) || ($this->forceCopy && !isset($options['forceCopy'])) || !is_dir($dstDir)) {
$opts = array_merge(
$options,
[
'dirMode' => $this->dirMode,
'fileMode' => $this->fileMode,
]
);
My 2nd problem after i run composer diagnose...i got message:
checking composer.json: FAIL..
actually what the problem of this...

Set linkAssets to false here:
https://github.com/trntv/yii2-starter-kit/blob/master/common/config/web.php#L6
It is a common problem about php symlink() function in Windows systems. There are many posts around the web about it

Related

How to fix api json delete request

In following code, I'm trying to create a new category (name = 'To be deleted') and then I want to delete it.
first part of the code working fine and I can see new record in the database every time I run the unit test
but I'm having a problem with recode deletion. what's wrong with my code
public function testCategoryDeletion()
{
$user = \App\User::find(1);
//dd($user->name);
$category = \App\Category::create(['name' => 'To be deleted']);
//dd($category->id);
$response = $this->actingAs($user, 'api')
->json('DELETE', "/api/category/{$category->id}")
->assertStatus(200)->assertJson([
'status' => true,
'message' => 'Category Deleted',
]);
}
Test case output
PS C:\xampp\htdocs\Chathura\Vue_Laravel_API> ./vendor/bin/phpunit
PHPUnit 7.5.7 by Sebastian Bergmann and contributors.
. . . . . . . 7 / 7 (100%)
Time: 1.53 minutes, Memory: 18.00 MB
OK (7 tests, 65 assertions)
PS C:\xampp\htdocs\Chathura\Vue_Laravel_API>
In database, recode is created but not deleted,
I fixed the controller as follows and now its working,
public function destroy(Category $category)
{
$status = $category->delete();
return response()->json([
'status' => $status,
'message' => $status ? 'Category Deleted' : 'Error Deleting Category'
]);
}
API routes
Route::group(['middleware' => 'auth:api'], function(){
Route::resource('/task', 'TaskController');
Route::resource('/category', 'CategoryController');
Route::get('/category/{category}/tasks', 'CategoryController#tasks');
});

Yii2 reCaptcha Widget Class Not Found

So I'm trying to use the Yii2 reCaptcha widget: http://www.yiiframework.com/extension/yii2-recaptcha-widget/
However, when setting the validation rule for it
['reCaptcha'], \himiklab\yii2\recaptcha\ReCaptchaValidator::className(), 'secret' => Yii::$app->params['reCAPTCHA.secretKey']
I get the error Class 'himiklab\yii2\recaptcha\ReCaptchaValidator' not found. I'm quite new to Composer and Yii2, so I'm not sure what I'm missing.
composer.json
{
"autoload" : {
"psr-4" : {
"Gaiatools\\Yii\\" : "src"
}
},
"name" : "gaiatools/site",
"require" : {
"php" : ">=7.0",
"himiklab/yii2-recaptcha-widget" : "*"
}
}
vendor/composer/autoload_psr4.php
// autoload_psr4.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'yii\\composer\\' => array($vendorDir . '/yiisoft/yii2-composer'),
'yii\\' => array($vendorDir . '/yiisoft/yii2'),
'himiklab\\yii2\\recaptcha\\' => array($vendorDir . '/himiklab/yii2-recaptcha-widget'),
'cebe\\markdown\\' => array($vendorDir . '/cebe/markdown'),
'Gaiatools\\Yii\\' => array($baseDir . '/src'),
);
Add this line to require section in project-directory/composer.json file
"himiklab/yii2-recaptcha-widget" : "*"
Then run in project directory command:
composer update
Run the following command in a terminal inside your project directory. It will be added automatically in composer.json file.
composer require --prefer-dist "himiklab/yii2-recaptcha-widget" "*"
Often you get the error of class not found when the file containing the class definition is not added in current code using namespaces.
Please ensure that you have added the required namespace in file where you are setting the validation rule with following code at the top of file
use himiklab\yii2\recaptcha
Hope this will solve your problem

How to output geoJSON file from mysql spatial table?

I have a mysql spatial database ('mydb1') of one table ('pk'), that i obtained from a point shapefile using ogr2ogr tool. one row of this 'pk' table, is like:
OGR_FID SHAPE CODIF Position_X Position_Y Nom Status
1 [GEOMETRY-25o] PAC182854 398235.38 414569.24 G-31 Vert
I need to output the geoJSON file of this table. I downloaded geoPHP library and used MySQL to GeoJSON script which i adapted to my settings, like below:
<?php
/**
* Title: MySQL to GeoJSON (Requires https://github.com/phayes/geoPHP)
* Notes: Query a MySQL table or view and return the results in GeoJSON format, suitable for use in OpenLayers, Leaflet, etc.
* Author: Bryan R. McBride, GISP
* Contact: bryanmcbride.com
* GitHub: https://github.com/bmcbride/PHP-Database-GeoJSON
*/
# Include required geoPHP library and define wkb_to_json function
include_once('geoPHP/geoPHP.inc');
function wkb_to_json($wkb) {
$geom = geoPHP::load($wkb,'wkb');
return $geom->out('json');
}
# Connect to MySQL database
$conn = new PDO('mysql:host=localhost;dbname=mydb1','root','admin');
# Build SQL SELECT statement and return the geometry as a WKB element
$sql = 'SELECT *, AsWKB(SHAPE) AS wkb FROM pk';
# Try query or error
$rs = $conn->query($sql);
if (!$rs) {
echo 'An SQL error occured.\n';
exit;
}
# Build GeoJSON feature collection array
$geojson = array(
'type' => 'FeatureCollection',
'features' => array()
);
# Loop through rows to build feature arrays
while ($row = $rs->fetch(PDO::FETCH_ASSOC)) {
$properties = $row;
# Remove wkb and geometry fields from properties
unset($properties['wkb']);
unset($properties['SHAPE']);
$feature = array(
'type' => 'Feature',
'geometry' => json_decode(wkb_to_json($row['wkb'])),
'properties' => $properties
);
# Add feature arrays to feature collection array
array_push($geojson['features'], $feature);
}
header('Content-type: application/json');
echo json_encode($geojson, JSON_NUMERIC_CHECK);
$conn = NULL;
?>
But when i execute the code on the browser, i get totally a blank page. What's wrong should i fix to output my geoJSON file plz?
You should check the php logs for more information in case an error ocured. Make sure error reporting and display are enabled:
error_reporting(E_ALL);
ini_set('display_errors', 1);
if no error show up inspect the $geojson object easiest would be using
print_r($geojson);

How can I call a console command in web application in Yii 2.0

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.

Joomla 3.1 Database update query not working

Recently I was making an upload component for joomla 3.1 back-end.
based on How to Save Uploaded File's Name on Database
I was successful in moving the file to the hard-drive,
however I just cant get the update query to work based on the posted post above.
I don't get any SQL errors and saving works, but somehow ignores the database part.
I really hope I missed something obvious. (btw I don't know the joomla way of queries very well)
In phpmyadmin the following query works:
UPDATE hmdq7_mysites_projects
SET project_file = 'test'
WHERE id IN (3);
I have tried the following queries:
$id = JRequest::getVar('id');
$db =& JFactory::getDBO();
$sql = "UPDATE hmdq7_mysites_projects
SET project_file =' " . $filename. "'
WHERE id IN (".$id.");";
$db->setQuery($sql);
$db->query();
$colum = "project_file";
$id = JRequest::getVar('id');
$data = JRequest::getVar( 'jform', null, 'post', 'array' );
$data['project_file'] = strtolower( $file['name']['project_file'] );
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->update('#__mysites_projects');
$query->set($column.' = '.$db->quote($data));
$query->where('id'.'='.$db->quote($id));
$db->setQuery($query);
$db->query();
Here is the current code:
class MysitesControllerProject extends JControllerForm
{
function __construct() {
$this->view_list = 'projects';
parent::__construct();
}
function save(){
// ---------------------------- Uploading the file ---------------------
// Neccesary libraries and variables
jimport( 'joomla.filesystem.folder' );
jimport('joomla.filesystem.file');
$path= JPATH_SITE . DS . "images";
// Create the gonewsleter folder if not exists in images folder
if ( !JFolder::exists(JPATH_SITE . "/images" ) ) {
JFactory::getApplication()->enqueueMessage( $path , 'blue');
}
// Get the file data array from the request.
$file = JRequest::getVar( 'jform', null, 'files', 'array' );
// Make the file name safe.
$filename = JFile::makeSafe($file['name']['project_file']);
// Move the uploaded file into a permanent location.
if ( $filename != '' ) {
// Make sure that the full file path is safe.
$filepath = JPath::clean( JPATH_SITE . "/images/" . $filename );
// Move the uploaded file.
JFile::upload( $file['tmp_name']['project_file'], $filepath );
$colum = "project_file";
$id = JRequest::getVar('id');
$data = JRequest::getVar( 'jform', null, 'post', 'array' );
$data['project_file'] = strtolower( $file['name']['project_file'] );
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->update('#__mysites_projects');
$query->set($column.' = '.$db->quote($data));
$query->where('id'.'='.$db->quote($id));
$db->setQuery($query);
$db->query();
}
// ---------------------------- File Upload Ends ------------------------
JRequest::setVar('jform', $data );
return parent::save();
}
(Answered by the OP in comments. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
Solved: after reviewing the post update record in database using jdatabase I made up some fixed test values. It turns out the query is correct but $data variable in the query had no data. $data['project_file'] = strtolower( $file['name']['project_file'] ); removed the array from first part and variable worked.