Styling ForbiddenHttpException error in yii2 - php

When I get a ForbiddenHttpException its just ugly black on white text. It's not using the standard pretty Yii2 error from the 'views/site/error.php'
An Error occurred while handling another error: exception
'yii\web\ForbiddenHttpException' with message 'You are not allowed to
perform this action.' in
/vendor/yiisoft/yii2/filters/AccessControl.php:151
Config has:
'errorHandler' => [
'errorAction' => 'site/error',
],
Is it possible to style ALL Yii2 errors to look the same?

First of all check for errorHandler in you main config
'errorHandler' => [
'errorAction' => 'site/error',
],
If it is not there it will show u a plain text.
And for Access Control Custom Error handling you can set a denyCallback in access behaviour
'denyCallback' => function($rule, $action){
// Your Code Goes Here
}

Related

How i can get log message with yii2

i have a little problem.
I'm developing a php application using Yii2 framework and i want to save a log messagges into db table.
I'm coding my own component witch extends DbTarget, in this component i rewrite export() function to save data into my table. It's works fine, but i can't get the log message.
For example, when i call Yii:log('message log'), all my data are saved in my db except 'message log' because i don't know how to get this value in my component.
Any solutions?
thanks
P.s. I'm newbee with yii2 and i have read the official documentation, but i didn't find any solution.
It seems you should specify level of the message
Yii:log('message log', Logger::LEVEL_TRACE);
or use shortcut methods
Yii::info('message log');
Yii::trace('message log');
Yii::error('message log');
Check your config for another targets. May be your message goes to level or category of another target. Notice that default category is 'application'.
To be shure you can make this configuration
'components' => [
'log' => [
'targets' => [
[
'class' => 'YourDbTarget',
'levels' => ['info'],
'categories' => ['application'],
],
],
],
],
And try to log info message
Yii::info('message log'); // target = info, category = application

ZF3 Navigation "plugin" error

Following https://docs.zendframework.com/zend-navigation/quick-start/, i try to make a navigation for my application. I registered a navigation, i added the DefaultNavigationFactory to the service-manager, but i get an error when i try to print the navigation.
This is my module/Application/config/module.config.php:
namespace Application;
use Zend\Navigation\Service\DefaultNavigationFactory;
use Zend\ServiceManager\Factory\InvokableFactory;
use Zend\View\Helper\Navigation;
return [
'navigation' => [
'default' => [
/* ... */
]
] ,
'service_manager' => [
'factories' => [
'navigation' => DefaultNavigationFactory::class,
],
],
];
But when $this->navigation('default')->menu(), i get this error, excluding the stack trace:
Fatal error: Uncaught Zend\ServiceManager\Exception\ServiceNotFoundException: A plugin by the name "navigation" was not found in the plugin manager Zend\View\HelperPluginManager in C:\Users\bikke_000\Documents\Sites\slapenenzo\vendor\zendframework\zend-servicemanager\src\AbstractPluginManager.php:133
Running
composer require zendframework/zend-navigation
fixed the issue it seems that in zf3 navigation isn't installed by default
In addition to the answer of #Ponsjuh I had to include the module "Zend\Navigation" in my modules config.
you need to pass 'navigation' instead of 'default' as below:
$this->navigation('navigation')->menu();
Check your "development mode". In production mode, 'config_cache_enabled' is true. When you install new module, you must refresh cache.
Default: 'data/cache/module-config-cache.application.config.cache.php'

How can I prevent post variables from appearing in the Yii2 log when I call warning() or info()?

I am maintaining an app built in Yii2, and I want to use Yii::warning() to write log messages. This is fine except when I'm logging events in the user login sequence.
The username and password are sent as POST variables. These are sensitive information which should not be captured in a log file.
$errorno = ldap_errno($this->link);
$errorstr = ldap_err2str($errorno);
Yii::warning("LDAP error: $errorno: $errorstr");
The above code causes a log warning to appear, containing my ldap error messages, but that warning contains a full stack trace and POST variables.
Even if the warning is only written when there is a problem with the LDAP connection, it could contain any user's credentials at that time, from server admin to CEO.
How can I log warnings for authentication related events in Yii without getting a full stack trace and dump of POST fields?
You can configure what PHP superglobal variables are exported to the log for each log target. In your config file, e.g.:
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
'logVars' => ['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION', '_SERVER'],
],
],
],
The default setting equivalent when the logVars property is omitted is shown in the example above.

Showing generic error pages to users in Yii 2

I'm using Yii 2 and I'm wondering if Yii has anything built in to handle generic error pages to show to users.
Like for example you may want to show them a general error page because their logout failed for some reason or a range of other reasons. Stuff that you don't want to have to create a view for every situation.
Is there something like this available and if so, how do you use it?
Both the basic and the advance apps come with it by default:
'components' => [
..................
'errorHandler' => [
'errorAction' => 'site/error',
],
..............
class SiteController extends Controller
............
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
You just have to throw an error now and you will see the page.
make sure YII_DEBUG is false.
if YII_DEBUG is true then error stack is displayed.
You can define YII_DEBUG By
1) index.php
//remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG', true);
//specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
OR
2).envFramework
YII_DEBUG = true
YII_ENV = dev

Yii not showing any error

Can't make Yii to show error to webbrowser. Configured custom error handler in config:
'errorHandler' => array(
// use 'site/error' action to display errors
'errorAction' => 'site/error',
),
But all errors only got written to application.log and then Apache servers empty page with error 500. How to make Yii print errors to the screen?
index.php:
// remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG', true);
// specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
You should a little bit modify your config/main.php. There is an item errorHandler. It should look like this:
'errorHandler' => [
// use 'site/error' action to display errors
'errorAction' => YII_DEBUG ? null : 'site/error',
],
This happened due to increased priority of CErrorHandler::$errorAction. Here is discussion: https://github.com/yiisoft/yii/issues/3760
It might be more of an Apache setting. What you have is correct.
Mine also has this one.
defined('YII_DEBUG_SHOW_PROFILER') or define('YII_DEBUG_SHOW_PROFILER',true);
You can also do this.
error_reporting(E_ALL);
ini_set("display_startup_errors","1");
ini_set("display_errors","1");
read runtime/logs/app.log
you can see any action and error that happens in you yii2 app.

Categories