How to configure 403 template in bjyauthorize - zend-framework2

I just setup the bjyoungblood/bjy-authorize and wonder how I can tell Zend Framework 2 where my error/403 template lies.
I didn't configure the initial setting of 'template' => 'error/403',
The 403.phtml file lies within the vendor directory but I get the following error message:
Warning: include(C:\myproject\config\autoload/../view/error/403.phtml) [function.include]: failed to open stream: No such file or directory in C:\myproject\vendor\zendframework\zendframework\library\Zend\View\Renderer\PhpRenderer.php on line 507
What is wrong with my configuration?

For performance reasons (mainly avoiding stat calls), BjyAuthorize uses the template map to define which file to use when the error/403 view is requested. This is an option of the view_manager settings, as described in the Zend\View documentation.
To set your own, you can simply define something like following in your config/autoload/your-settings.local.php:
'view_manager' => array(
'template_map' => array(
'error/403' => '/absolute/path/to/your/error/403.phtml',
),
),
Or, in your module config:
'view_manager' => array(
'template_map' => array(
'error/403' => __DIR__ . '/view/error/403.phtml',
),
),
I suggest always providing absolute paths for configuration, so be sure that your file C:\myproject\config\autoload/../view/error/403.phtml is the correct path.
You can also use a different view for 403 errors if you prefer to do so. That can be achieved by changing $config['bjyauthorize']['template']:
'bjyauthorize' => array(
'template' => 'my-module/unauthorized-template',
),

You can manually set:
'error/403' => __DIR__ . '/../view/error/403.phtml',
in module/Application/config/module.config.php and comment out this line in module.bjyauthorize.global.php. I'm assuming you are working with the skeleton.
#aravind.udayashankara, BjyAuthorize does work with composer and the skeleton already comes configured to load modules in the vendor as well, so no need to move it in modules directory.
However, it does seem that phprender isn't able to access the view directory in the vendor. It might be the way "error/403' => DIR . '/../view/error/403.phtml" is actually mapped/interpreted at runtime.

Related

Translations in sourceLanguage does not work in Yii2 application

I'm using a translations based on keywords in my Yii2 application (I know, that this isn't best option, but I don't have other). I've prepared #app/messages/pl/app.php and #app/messages/en/app.php files with translation strings using keywords, instead of full-featured sentences or words:
<?php
return [
'name_english'=>'Name in English',
'keywords_english'=>'Keywords in English'
];
?>
I have set my application to use Polish language as default:
'language' => 'pl',
'sourceLanguage' => 'en',
And I'm invoking translation:
Yii::t('app', 'keywords_english');
Everything works fine, when language is actually set to base, Polish (pl):
But, when I change it to English (en; either by setting Yii::$app->language during runtime or by changing application configuration), translation is not performed and I'm getting keywords_english:
I have put die() in the beginning of #app/messages/pl/app.php and #app/messages/en/app.php files and I can clearly see, that when language is set to English, second file is not being included by Yii2 (application run follows), while, when language is Polish, first file is included and application flow breaks on that die().
What am I missing? Why Yii2 is not using translations from #app/messages/en/app.php file, if language is set to English (en)?
EDIT: By default, I was not altering default i18n component configuration in my application's configuration as I found no need for that. Translation files are stored in default position (#app/messages/<language>/) and are using default class (yii\i18n\PhpMessageSource). And this is working for all languages except sourceLanguage. At some point, I tried to alter configuration:
'i18n' => [
'translations' => [
'*' => [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en',
'basePath' => '#app/messages'
],
],
],
But, it brought no change (why should it -- it still uses default settings).
According to samdark at Yii Forum, this is by design. Translations are not performed, if language = sourceLangage.
To workaround this, and force translations in this case, one must set forceTranslation to true.
Therefore, one must add / modify i18n component in components section of application's config in the way similar to this:
'i18n' => [
'translations' => [
'app' => [
'class' => 'yii\i18n\PhpMessageSource',
'forceTranslation' => true
],
],
],
Solution:
'translations' => [
'app*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#app/messages',
'sourceLanguage' => 'en_US',
'fileMap' => [
'app' => 'app.php',
],
],
Answers for your comment:
1) 'sourceLanguage' => 'en_US' - You must use full locale. Because English locale may be en_US, en_UK and e.t.c. The format for the language/locale is ll-CC where ll is a two- or three-letter lowercase code for a language according to ISO-639 and CC is the country code according to ISO-3166. from [doc][1]
2) In key use category. And category set in Yii::t('category'...)

Zend Framework GData Youtube Loading issue

I am trying to use Gdata package of Zend-2 framework to access Youtube API.
I have a successfully working version with Zend 1.9 version. I am trying to port them to Zend 2 framework version.
The folder structure is
C:\wamp\www\plugins\youtube\
C:\wamp\www\plugins\youtube\Zend\ (all default folders that comes with ZF2)
C:\wamp\www\plugins\youtube\Zend\ZendGData (downloaded separately from Zend Packages page)
I have added the path C:\wamp\www\plugins\youtube\ to the include_path by using set_include_path() function and have verified the same.
I am using the below code to create the YouTube object.
$yt = new ZendGData\YouTube();
I am getting the below error.
Class 'ZendGData\YouTube' not found
I am not how to use the auto-loading feature of ZF2. I tried to include the Loader/StandardAutoloader.php file. But still the same.
If I include the Zend\ZendGData\YouTube.php file I get the notice that the ZendGData\Media is not found.
Please let me know if I am missing something silly.
EDIT:
Some more information on what I have done now.
Based on search from Stackoverflow site, I did the below changes.
use Zend\Loader\StandardAutoloader;
use ZendGdata\YouTube;
require_once 'C:\wamp\www\plugins\youtube\Zend\Loader\StandardAutoloader.php';
$loader = new StandardAutoloader(array('autoregister_zf' => true));
$loader->register();
$yt = new Zend\ZendGData\YouTube();
Now I get the below error.
Class 'ZendGData\Media' not found
After a lot of trial and error with the AutoLoader, the below code worked for me. If someone is having the same issue, here is the solution.
require_once 'C:\wamp\www\youtube\Zend\Loader\StandardAutoloader.php';
$loader = new StandardAutoloader(array(
'autoregister_zf' => true,
'namespaces' => array(
'ZendGData' => 'C:\wamp\www\youtube\Zend\ZendGdata\')
)
);
$loader->register();
$yt = new ZendGData\YouTube();

Failed to open - Zend Framework

Warning: require_once(Zend/Application.php) [function.require-once]:
failed to open stream: No such file or directory in
D:\xampp\htdocs\obsessa\public\index.php on line 17
Fatal error: require_once() [function.require]: Failed opening
required 'Zend/Application.php'
(include_path='D:\xampp\htdocs\obsessa\library;.;D:/xampp/php/PEAR;D:/xampp/php/ZendFramework')
in D:\xampp\htdocs\obsessa\public\index.php on line 17
Hi Anyone help me on this ?
D:/xampp/php/PEAR;D:/xampp/php/ZendFramework
Should probably be:
D:/xampp/php/PEAR;D:/xampp/php/ZendFramework/library
The immediate problem is that your php include_path configuration is wrong. The reason is the way PSR-0 autoloading works. Your include path has two paths. If you want to load Zend\Application, then you need an include path that has a subdirectory called 'Zend'. In your case, D:/xampp/php/ZendFramework does not have that, you need D:/xampp/php/ZendFramework/library (which has a subdirectory named Zend, which has a file named Application.php)
But really, if you're doing ZF2, grab the skeleton application and use composer, just like the docs say, especially if you're new. composer is fantastic, and it's the preferred way to go. With composer, each application you write has its own installation of zf2. There are very few situations where you want your apps to rely on some globally-installed framework, which is the setup you're trying to do.
Please follow this step :
Please donwload the zend libray from zend official site
http://framework.zend.com/
Then paste donwloaded library in zend libray folder
Index.php file settings
//Ensure library/ is on include_path
set_include_path(
implode(PATH_SEPARATOR,
array(realpath(APPLICATION_PATH . '/../library'),
get_include_path(),)));
/** Zend_Application */
require_once 'Zend/Application.php';
please set index.php file as:
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(FILE) . '/../application'));
defined('LIBRARY_PATH')
|| define('LIBRARY_PATH', realpath(dirname(FILE) . '/../library'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
define('PUBLIC_PATH','http://'.$_SERVER['HTTP_HOST'].'/demo/public');
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
set_include_path(implode(PATH_SEPARATOR, array(
realpath(DOCTRINE_PATH),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();

Dynamic CSS using Sprockets

I'm working on a site builder in rails and I would like to render the sites css using Sprockets SCSS processors. Since the user can change colors and logos, I can't use Sprockets precompilation so I've started working on a Rails SCSS template handler to handle dynamic views.
The goal is to compile 'app/views/sites/show.css.scss' any time /sites/43543.css is requested. Here's what I have so far. You'll notice I first run the template through the ERB processor and then attempt to run it through Sprockets.
https://gist.github.com/3870095
Manuel Meurer came up with an alternative solution that writes the ERB output to a path and then triggers the Asset Pipeline to compile it. I was able to get his solution to work locally but it wont work on heroku because the asset path is not writable. Files can only be written to the tmp directory and those files are only guaranteed for a single request.
http://www.krautcomputing.com/blog/2012/03/27/how-to-compile-custom-sass-stylesheets-dynamically-during-runtime/
After a long day I was able to solve my problem thanks to John Feminella and his post on google. The challenging part for me was figuring out how to create a new Sprockets::Context. Luckily John's solution doesn't require a Context.
Updated gist here
Attempt #1
This code does not allow importing css files from the asset pipeline.
#import "foundation"; works because foundation is loaded as a compass module
#import "custom_css"; results in an error message saying the file could not be found
def call(template)
erb = ActionView::Template.registered_template_handler(:erb).call(template)
%{
options = Compass.configuration.to_sass_engine_options.merge(
:syntax => :scss,
:custom => {:resolver => ::Sass::Rails::Resolver.new(CompassRails.context)},
)
Sass::Engine.new((begin;#{erb};end), options).render
}
end
Attempt #2
This code fails to embed base64 urls using asset-data-url
def call(template)
erb = ActionView::Template.registered_template_handler(:erb).call(template)
%{
compiler = Compass::Compiler.new *Compass.configuration.to_compiler_arguments
options = compiler.options.merge({
:syntax => :scss,
:custom => {:resolver => ::Sass::Rails::Resolver.new(CompassRails.context)},
})
Sass::Engine.new((begin;#{erb};end), options).render
}
end
Attempt 3
Turns out you can use empty values while creating the context. Code below works in development but throws an error in production.
ActionView::Template::Error (can't modify immutable index)
It appears the error occurs in Sprockets::Index which is used instead of Sprockets::Environment in production. Switching to Sprockets::Environment doesn't solve the problem either.
def call(template)
erb = ActionView::Template.registered_template_handler(:erb).call(template)
%{
context = CompassRails.context.new(::Rails.application.assets, '', Pathname.new(''))
resolver = ::Sass::Rails::Resolver.new(context)
compiler = Compass::Compiler.new *Compass.configuration.to_compiler_arguments
options = compiler.options.merge({
:syntax => :scss,
:custom => {:resolver => resolver}
})
Sass::Engine.new((begin;#{erb};end), options).render
}
end

rails 3, xml formatting and builder

I have an xml tag that needs to be formatted like so:
<AddDealRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
I can't seem to get this to work properly, using builder. I am attempting the following code in builder:
xml.AddDealRequest(:xmlns:xsi => "http://www.w3.org/2001/XMLSchema-instance", :xmlns:xsd => "http://www.w3.org/2001/XMLSchema" ) do
but obviously that second colon is throwing off the symbol. Is there any way to escape that second symbol? Or is this declaration entirely necessary?
Thanks!
Try quoting your symbols:
xml.AddDealRequest(
:'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
:'xmlns:xsd' => "http://www.w3.org/2001/XMLSchema"
)
You could also try using strings instead of symbols
xml.AddDealRequest(
'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
'xmlns:xsd' => "http://www.w3.org/2001/XMLSchema"
)
but I don't know if builder will be happy with that but the documentation includes things like this:
xm.target("name"=>"compile", "option"=>"fast")
# => <target option="fast" name="compile"\>
so strings for the attribute names should work.
A bit of time in irb might be help clarify things:
>> 'where_is:pancakes_house'.to_sym
=> :"where_is:pancakes_house"
>> :'xmlns:xsi'.to_s
=> "xmlns:xsi"
Rather than expect anyone to read through all the comments in the earliest answer, I'll just post the outcome here:
Firefox doesn't display the xmlns attribute (at least not when it matches a default). If you view the source (Ctrl+U) or use Chrome as your browser, you'll see that the missing attributes are appearing in the xml output.

Resources