I am using appendTimestamp of assetManager component
'assetManager' => [
//append time stamps to assets for cache busting
'appendTimestamp' => true,
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
It correctly adds the timestamp after each asset as shown:
<link href="/frontend/web/assets/7b3fec74/css/arabic.css?v=1428761706" rel="stylesheet">
However when I make changes to that CSS file, the timestamp does not update. Is this because of the FileCache?
Every time I wish to test my new changes, I currently need to clear the contents of my web/assets folder
Am I required to delete the contents of the assets folder every time I wish to test my new assets?
I had same problem when I used $sourcePath as files source in my asset bundle. I solved it buy adding $publishOptions. Making 'forceCopy'=>true forces files to be published in assets folder each time:
class Asset extends AssetBundle
{
public $sourcePath = '...';
public $js = [..];
public $css = [...];
public $depends = [...];
public $publishOptions = [
'forceCopy' => true,
//you can also make it work only in debug mode: 'forceCopy' => YII_DEBUG
];
}
The FileCache component you referred to - has nothing to do with the assets. It is responsible with your defined cache items :
Yii::$app->cache->set('key', 'value')
Yii::$app->cache->get('key')
...
So there might be a problem with your assetManager.
Related
I'm trying to set up a zend framework 3 MVC web app to use session storage. Following the information from this website --
https://olegkrivtsov.github.io/using-zend-framework-3-book/html/en/Working_with_Sessions/PHP_Sessions.html
It all works well. I get the session variable in my controller and I can save data to the session container just fine. The problem is, the data I save to the container is NOT there on subsequent calls. I'm saving search criteria from one page and doing a redirect to a second page to do the search and return the results. The session data is not present when I enter the second page.
In config\global.php I have --
return [
'session_config' => [
// Cookie expires in 1 hour
'cookie_lifetime' => 60*60*1,
// Stored on server for 30 days
'gc_maxlifetime' => 60*60*24*30,
],
'session_manager' => [
'validators' => [
RemoteAddr::class,
HttpUserAgent::class,
],
],
'session_storage' => [
'type' => SessionArrayStorage::class,
],
];
In application\module.php i have modified onBoostrap
public function onBootstrap(MvcEvent $event)
{
$application = $event->getApplication();
$svcMgr = $application->getServiceManager();
// Instantiate the session manager and
// make it the default one
//
$sessionManager = $svcMgr->get(SessionManager::class);
}
I created an IndexControllerFactory
class IndexControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container,
$requestedName, array $options = null)
{
// Get access to session data
//
$sessionContainer = $container->get('Books\Session');
return new IndexController($sessionContainer);
}
}
Modified my IndexController to add a constructor method
class IndexController extends AbstractActionController
{
private $session;
public function __construct(Container $session)
{
$this->session = $session;
}
In application\module.config.php i have this
'controllers' => [
'factories' => [
Controller\IndexController::class => Controller\Factory\IndexControllerFactory::class,
],
],
'session_containers' => [
'Books\Session'
],
To store something in session you can create the container as follows:
// Create a session container
$container = new Container('Books\Session');
$container->key = $value;
To retrieve something from a session container later you have to create a new container with the same name:
// Retrieve from session container
$container = new Container('Books\Session');
$value = $container->key;
As far as I know this works similarly for both ZF2 and ZF3 and can be found in other posts on StackOverflow or for example this blog post with the title Using Sessions in Zend Framework 2 .
If you create a new Container for storing or resolving data from the session it will automatically use the default session manager if you don't pass one yourself.
You can see that here in the AbstractContainer::__construct method on line 77. If the $manager passed to the constructor is null it will get the default session manager inside the setManager method.
So to use sessions you don't need to do a lot of manual configuration.
If that doesn't solve your problem please leave a comment.
I've followed step by step instructions from JasperReports for yii2.
Installed JDK 1.8 on my Debian 8
Setted up mysql connector's classpath in /etc/profile
Added chrmorandi/yii2-jasper to my composer and updated
I guess php exec() function is enabled because the following test in any view resolves successfuly ...
<? echo exec('whoami'); ?>
chromandi is now under /vendors
java -version says 1.8.0_111
$CLASHPATH points to /usr/share/mysql-connector-java/mysql-connector-java-5.1.40-bin.jar
configuration is so ...
'components' => [
'jasper' => [
'class' => 'chrmorandi\jasper',
'db' => [
'host' => 'localhost',
'port' => 3306,
'driver' => 'mysql',
'dbname' => 'acme',
'username' => 'acme',
'password' => 'acme'
]
],
...
]
I added a controller like this ...
<?php
namespace app\controllers;
use Yii;
use chrmorandi\Jasper;
class EstadisticasController extends \yii\web\Controller {
public function actionIndex() {
// Set alias for sample directory
Yii::setAlias('example', '#vendor/chrmorandi/yii2-jasper/examples');
/* #var $jasper Jasper */
$jasper = Yii::$app->jasper;
// Compile a JRXML to Jasper
$jasper->compile(Yii::getAlias('#example') . '/hello_world.jrxml')->execute();
// Process a Jasper file to PDF and RTF (you can use directly the .jrxml)
$jasper->process(Yii::getAlias('#example') . '/hello_world.jasper', [
'php_version' => 'xxx'
], [
'pdf',
'rtf'
], false, false)->execute();
// List the parameters from a Jasper file.
$array = $jasper->listParameters(Yii::getAlias('#example') . '/hello_world.jasper')->execute();
// return pdf file
Yii::$app->response->sendFile(Yii::getAlias('#example') . '/hello_world.pdf');
}
}
and tested http://www.acme.es/estadisticas/index that is supposed to be a built-in example.
Now it comes the problem. It complains about
$jasper = Yii::$app->jasper;
line. The output says
ReflectionException Class chrmorandi\jasper does not exist
Anyone facing this issue? There is no much info about jasper on Yii. Any help would be welcome.
Thank you.
Finally the solution was changing
$jasper = Yii::$app->jasper;
to
$jasper = new \chrmorandi\jasper\Jasper();
Don't know why in the yii2-jasper documentation is setted so if it does not work. Anyway you can make it work following my above compilation.
As the
use chrmorandi\Jasper
is not working properly
You will have to change Jasper.php setting in the init function this
$componentes = Yii::$app->components;
$this->db = $componentes['jasper']['db'];
to make it work.
Editing under vendors is not something I want to do. In order to prevent these fixes I moved from chrmorandi's extension (until its improvement) to cossou/jasperphp extension.
So far the cossou's extension reaches all my goals.
I hope this helps somebody.
I am having trouble storing images in the 'public' folder of the Laravel framework (which I believe is the static content folder? Please correct me if I am wrong).
I am running a seed using faker which generates images and stores the images URL's in a table. This is all working correctly but the seed itself keeps failing as it can't seem to access or find the folder within the public folder I have created. This is my public folder structure:
/public/assets/images
And here is my seed which should save my images into this folder:
<?php
use Illuminate\Database\Seeder;
class ProductsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
DB::table('products')->truncate();
$faker = Faker\Factory::create();
$limit = 30;
for($i = 0; $i < $limit; $i++) {
DB::table('products')->insert([
'title' => $faker->name,
'SKU' => $faker->name,
'description' => $faker->text,
'created_at' => $faker->dateTime,
'updated_at' => $faker->dateTime,
'images' => $faker->image(URL::to(Config::get('assets.images')) ,800, 600, [], [])
]);
}
}
}#
The 'assets.images' config file is:
<?php
return [
'images' => '/assets/images'
];
When I try and run the seed to populate my database I get the following error:
[InvalidArgumentException]
Cannot write to directory "http://localhost:8931/assets/images"
I cannot see where I am going wrong. Can anyone offer any insight?
Change your call of URL::to with the helper function public_path() like this:
$faker->image(public_path(Config::get('assets.images')) ,800, 600, [], [])
I'm not a laravel expert, but the thrown exception looks like you're trying to write to an url rather than a file path on the disk.
Maybe you want to check if the process owner running the php script has write access to that location.
now the default aliaes
#vender Defaults to #app/vendor.
how to change it defaults to #webroot/vendor.
ignore below info
when using assetsBundle in the namespace repo.
namespace repo\assets;
use yii\web\AssetBundle;
class RepoAsset extends AssetBundle {
public $sourcePath = '#repo/media';
public $css = [
'css/site.css',
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
I can't publish the depends asset.
The file or directory to be published does not exist: /Users/tyan/Code/php/myii/repo/vendor/bower/jquery/dist
but the real location of the jquery is
/Users/tyan/Code/php/myii/vendor/bower/jquery/dist
it this happends to bootstrapAsset too.I try to echo the
#app //seems it's location is /Users/tyan/Code/php/myii/repo
#vendor //it's /Users/tyan/Code/php/myii/repo/vendor
seems that #app and #vendor add my own namespace in it automatically which not make sence.
BTW. I define the #repo aliases in the web.php config file.
'aliases' => [
'#repo' => dirname(__DIR__),
],
'controllerNamespace' => 'repo\\controllers',
because I want to change the default namespace.
I need to optionally include some modules in my ZF2 app. The modules are entirely independent with any loaded modules.
In application.config.php, in the config array I can just include the main modules, and then, at the end, based on some conditions, to add the optional module. Like this:
$config = array(
'modules' => array(
'Application',
),
...
);
if (condition) {
$config['modules'][] = 'OptionalModule';
}
return $config;
Though this works and fixes the problem, I was wondering if there is another way of doing this.
Is it a good approach for this use case? Would there be a nicer way to accomplish this?
Thanks!
I usually do this by using any of the below two methods:
Conditionally load module with local application config
application.config.php:
<?php
use Zend\Stdlib\ArrayUtils;
$config = [
// You config
];
$local = __DIR__ . '/application.config.local.php';
if (is_readable($local)) {
$config = ArrayUtils::merge($config, require($local));
}
return $config;
application.config.local.php:
<?php
return [
// Your config
];
This enables you to have a base application config and load an additional config per deployment. So no if $condition, this is determined by your deployment process, which is most times easier to manage.
Note this also works for deployment configs: application.config.development.php vs application.config.production.php. This is just whatever you like, to suit your needs.
Conditionally execute code in module config
In your Module.php
<?php
namespace MyModule;
use Zend\Mvc\MvcEvent;
class Module
{
public function onBootstrap(MvcEvent $e)
{
$app = $e->getApplication();
$sm = $app->getServiceManager();
$config = $sm->get('Config');
if ($config['mymodule']['enabled'] === true) {
// condition
}
}
}
Then you can have your module.config.php in your own module folder:
<?php
return [
'mymodule' => [
'enabled' => true,
],
];
But if youn need to disable this in a certain environment, you add this to your config/autoload/local.php:
<?php
return [
'mymodule' => [
'enabled' => false,
],
];