yii2-advanced:how can i save image in backend and view that in backend and frontend? - yii2

hi i am save image in frontend and that show in frontend true and i test with many way to view that in backend but don't work.
please help me
my controller in backend
Yii::$app->params['uploadPath'] = Yii::getAlias('#frontend') .'/web/uploads/';
$path = Yii::$app->params['uploadPath'] . $model->image_web_filename;
$image->saveAs($path);
url my backend and frontend is seperate
backend:yii.com/:81
frontend:yii.com
i test these soloution but didn't work true:
https://stackoverflow.com/questions/23155428/how-to-get-root-directory-in-yii2
i inset two alias in aliases file in backend\config:
Yii::setAlias('#frontend', 'http://frontend.sample.dev');
Yii::setAlias('#backend', 'http://backend.sample.dev');
and use that in backend/web/index.php
require(__DIR__ . '/../config/aliases.php');
but i get this error:
An Error occurred while handling another error:
exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request "site/error".' in
/var/www/blog/vendor/yiisoft/yii2/base/Module.php:532
Stack trace:
#0 /var/www/blog/vendor/yiisoft/yii2/web/ErrorHandler.php(95):
yii\base\Module->runAction('site/error')
#1 /var/www/blog/vendor/yiisoft/yii2/base/ErrorHandler.php(111):
yii\web\ErrorHandler->renderException(Object(yii\web\NotFoundHttpException))
#2 [internal function]: yii\base\ErrorHandler-
>handleException(Object(yii\web\NotFoundHttpException))
#3 {main}
Previous exception:
exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request "post/index".' in
/var/www/blog/vendor/yiisoft/yii2/base/Module.php:532
Stack trace:
#0 /var/www/blog/vendor/yiisoft/yii2/web/Application.php(102):
yii\base\Module->runAction('post/index', Array)
#1 /var/www/blog/vendor/yiisoft/yii2/base/Application.php(380):
yii\web\Application->handleRequest(Object(yii\web\Request))
#2 /var/www/blog/backend/web/index.php(18): yii\base\Application->run()
#3 {main}
Next exception 'yii\web\NotFoundHttpException' with message 'Page not
found.' in /var/www/blog/vendor/yiisoft/yii2/web/Application.php:114
Stack trace:
#0 /var/www/blog/vendor/yiisoft/yii2/base/Application.php(380):
yii\web\Application->handleRequest(Object(yii\web\Request))
#1 /var/www/blog/backend/web/index.php(18): yii\base\Application->run()
#2 {main}

i'm pretty late to the party
but i got a few bones to pick with this solution provided.. so here it goes:
you mention you have different front and back configurations
so you are serving different folders for yii.com/:80 and yii.com/:81 respectively /frontend/web and /backend/web.
keeping this in mind,
no amount of aliases can make content of one of them available to the other.
the yii-advanced-app has #frontend and #backend aliases defined in common/config/bootstrap
Yii::setAlias('#common', dirname(__DIR__));
Yii::setAlias('#frontend', dirname(dirname(__DIR__)) . '/frontend');
Yii::setAlias('#backend', dirname(dirname(__DIR__)) . '/backend');
Yii::setAlias('#console', dirname(dirname(__DIR__)) . '/console');
DO NOT CHANGE THESE unless you know very well what you're doing.
Yii is using these aliases to autoload classes defined under these namespaces common, frontend, backend and console. this exact thing is causing the ridiculous chain of "errors occurring while handling other errors"
more details on Yii autloading documentaion
you can simply share content of these folders by creating a symlink from frontend/web/uploads to backend/web/uploads.
your webserver config (or .htaccess) will require the +FollowSymLinks option
Yii can manage this if you add a few lines to the environments/index.php file
'Development' => [
// .. other options
'setWritable' => [
// .. leave the default stuff there
'frontend/web/uploads',
],
'createSymlink' => [
// link => real folder
'backend/web/uploads' => 'frontend/web/uploads',
]
],
'Production' => [
// ..
'setWritable' => [
// ..
'frontend/web/uploads',
],
'createSymlink' => [
// link => real folder
'backend/web/uploads' => 'frontend/web/uploads',
]
],
and then run the init command again, just like in the installation guide (you do have to option to not overwrite local config files)
if you are running under windows, this might now ask you for elevated privileges
php init
or if you chose to make the symlinks manually you can try this quick guide

you can try this solution :
Yii::setAlias('#frontend', 'http://frontend.sample.dev');
Yii::setAlias('#backend', 'http://backend.sample.dev');
and if you upload files in backend set the src parameter of image to
Yii::getAlias('#backend/path/to/your/image/file');
and if you save your files in frontend replace #backend with #frontend

create this function in components folder
namespace common\components;
use Yii;
class Helper extends \yii\web\Request {
public static function getFrontendUrl($path) {
$frontUrl = str_replace('/adminpanel', '', $path);
return $frontUrl;
}
}
and use in backend :
$path \common\components\Helper::getFrontendUrl(Yii::$app->request->baseUrl).$img;
notic: in frontend You do not need

Related

Yii2 - Extend a controller into an exist module

I want to extend a controller from the backend / controller into a my existed module.
The structure of directory in My Yii2 Application as follows.
``
backend
controllers
JobOrderController
view
job-order
modules
marketing
controllers
JobOrderController [extend from #backend \ controllers \ JobOrderController]
``
When I access the route: localhost / marketing / job-order, I get an error message:
``
View not Found - yii \ base \ ViewNotFoundException
The view file does not exist:
../../advanced/backend/modules/marketing/views/job-order/index.php
``
I don't want to change any view from the marketing module, is it possible?
Just use controllerMap in Module config.
Also set the view folder.
public function init()
{
parent::init();
// custom initialization code goes here
$this->controllerMap = [
'job-order' => [
'class' => 'backend\components\controllers\JobOrderController',
'viewPath' => Yii::getAlias('#backend') . '/components/views/job-order'
]
];
}

Error reported while running Laucher by chisel

I downloaded the chisel-tutorial which is offered on the website of usb-bar.
In order to do practise I created a scala file named as "Regfile.scala" under the path:
"chisel-tutorial/src/main/scala/solutions/Regfile.scala".
The Test-file is stored under the path :
"chisel-tutorial/src/test/scala/solutions/RegfileTests.scala".
While running the sbt I was reported
(after execution of command "test:run-main solutions.Launcher Regfile"):
"Errors: 1: in the following tutorials
Bad tutorial name: Regfile "
How can I solve this problem?
You have to add your Regfile to Launcher.scala. The launcher is available in directory :
src/test/scala/solutions/Launcher.scala
I think you can add somethings like this to Launch.scala to test your Regfile:
"Regfile" -> { (backendName: String) =>
Driver(() => new Regfile(), backendName) {
(c) => new RegfileTests(c)
}
},

Symfony No Route To Host on edit, but works fine on findAll

I am using Symfony 3.0.4. I have the error
[2016-05-20 08:50:26] request.CRITICAL: Uncaught PHP Exception Doctrine\DBAL\Exception\ConnectionException: "An exception occured in driver: SQLSTATE[HY000] [2002] No route to host" at /var/www/WebProduction/products.markettraders.com/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php line 103 {"exception":"[object] (Doctrine\\DBAL\\Exception\\ConnectionException(code: 0): An exception occured in driver: SQLSTATE[HY000] [2002] No route to host at /var/www/WebProduction/products.markettraders.com/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php:103, Doctrine\\DBAL\\Driver\\PDOException(code: 2002): SQLSTATE[HY000] [2002] No route to host at /var/www/WebProduction/products.markettraders.com/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:47, PDOException(code: 2002): SQLSTATE[HY000] [2002] No route to host at /var/www/WebProduction/products.markettraders.com/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:43)"} []
[2016-05-20 08:50:26] security.DEBUG: Stored the security token in the session. {"key":"_security_main"} []
This is strange because the indexAction is working perfectly, only the Edit Action is NOTWorking, on all of my edit routes. There have been no changes in the code to cause this to happen.
What have I inadvertantly changed in my MySQL configuration that allows Doctrine to find everything on the indexAction but then error out on the editAction?
EDIT
Forgive me.... The code is below. It works in my local environment. It does not work in prod. The error above comes from the prod.log.
In addition the indexAction controller works as well.
/**
* Displays a form to edit an existing AOD Technical Analysis page entity.
*
* #Route("/{id}/edit", name="aod_technical_analysis_edit")
* #Method({"GET", "POST"})
*/
public function editAction(Request $request, AodTechnicalAnalysis $aod_tech)
{
$deleteForm = $this->createDeleteForm($aod_tech);
$editForm = $this->createForm('AppBundle\Form\AodTechnicalAnalysisType', $aod_tech);
$editForm->remove('currencypair');
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($aod_tech);
$em->flush();
$session = $request->getSession();
$message = 'The change was succesfully saved for ' . $aod_tech->getCurrencypair();
$session->getFlashBag()->add('success', $message);
return $this->redirectToRoute('aod_technical_analysis_index');
}
return $this->render('aod_tech/edit.html.twig', array(
'aod_tech' => $aod_tech,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
Have you tried appending "app_dev.php" on your URL to get the debug web toolbar? I have found this to be more helpful compared to logs. You might need to edit the "web/app_dev.php" to add your browser's IP address.

Kohana throws errors while adding filter to routes

I'm using Kohana framework v3.3.1. Here's the default route in my bootstrap.php,
Route::set('default', '(<controller>(/<action>(/<id>)))')
->filter(
function(\Route $route, $params, \Request $request) {
$params['action'] = str_replace('-', '_', $params['action']);
return $params;
}
)
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
Whenever I add "filter" to the route, I get the following error,
Fatal error: Uncaught Kohana_Exception [ 0 ]: Invalid Route::callback specified ~ SYSPATH/classes/Kohana/Route.php [ 391 ] thrown in /system/classes/Kohana/Route.php on line 391
The same code works fine in my localhost (ubuntu 14.04) but doesn't work in Bluehost. Any help would be greatly appreciated.
Figured it out. Though bluehost implements PHP v5.4, code corresponding to v5.2 only works.
Route::set('testing', 'foo')
->filter(array('Class', 'method_to_process_my_uri'));
Reference: Kohana v3.3 User Guide

Yii2 console Fatal error: Uncaught exception

When I enter command yii or test/codeception/bin/yii migrate in the console, receive such a message
Fatal error: Uncaught exception 'yii\base\UnknownPropertyException' with message 'Setting unknown property: yii\console\ErrorHandler::errorAction' in D:\Desktop\loalhost\yii2-wiersz\vendor\yiisoft\yii2\base\Component.php:197
Stack trace:
#0 D:\Desktop\loalhost\yii2-wiersz\vendor\yiisoft\yii2\BaseYii.php(518): yii\base\Component->__set('errorAction', 'site/error')
#1 D:\Desktop\loalhost\yii2-wiersz\vendor\yiisoft\yii2\base\Object.php(105): yii\BaseYii::configure(Object(yii\console\ErrorHandler), Array)
#2 [internal function]: yii\base\Object->__construct(Array)
#3 D:\Desktop\loalhost\yii2-wiersz\vendor\yiisoft\yii2\di\Container.php(372): ReflectionClass->newInstanceArgs(Array)
#4 D:\Desktop\loalhost\yii2-wiersz\vendor\yiisoft\yii2\di\Container.php(151): yii\di\Container->build('yii\\console\\Err...', Array, Array)
#5 D:\Desktop\loalhost\yii2-wiersz\vendor\yiisoft\yii2\BaseYii.php(344): yii\di\Container->get('yii\\console\\Err...', Array, Array)
#6 D:\Desktop\loalhost\yii2-wiersz\vendor\yiisoft\yii2\di\ServiceLocator.php(13 in D:\Desktop\loalhost\yii2-wiersz\vendor\yiisoft\yii2\base\Component.php on line 197
console/config/main.php is default. Problem is on localhost (win7x62) and remote host (debian).
I had the same situation with the attempt to migrate rbac
(yii migrate --migrationPath=#yii/rbac/migrations)
What is causing the problem ??? my ignorance ;)?
Well, as you error message says, you are trying to set unknown property 'errorAction'. I suppose you are using the same error component config here in console app as in web app. See if there is
[
'components' => [
'error' => [
'errorAction' => ...
]
]
]
in your console app config. There shouldn't be 'errorAction'.
Thanks,
I moved from frontend/config and backend/config code
'errorHandler' => [
'errorAction' => 'site/error',
]
to common/config. Frontend and backend app did not have a problem with that, but console yes. After rollback is ok.