yii custom error pages like 404, 403, 500 - yii

I'm trying to have separate files for all my error messages. (404, 403, 500 etc) so i can have custom designs for them. If possible i don't want the header and footer to be included in my error pages too. Right now i have this, in my SiteController.php and put error404.php into my views/site/ folder
public function actionError()
{
$error = Yii::app()->errorHandler->error;
switch($error['code'])
{
case 404:
$this->render('error404', array('error' => $error));
break;
.......
.......
}
}
i was wondering if there is a better way? or if Yii has way to handle this that i'm missing.
i read this page http://www.yiiframework.com/doc/guide/1.1/en/topics.error
and it says something about putting files into /protected/views/system but i don't quite understand Yii's documentation.

As you read Yii will look for files in /protected/views/system (after looking in themes if you have any)
You do not need to write any fresh actions all you have to do is create a folder system in the views directory and create files named errorXXX.php XXX being the error code.
The default page looks like this you modify this as you wish and save it in /protected/views/system
<body>
<h1>Error <?php echo $data['code']; ?></h1>
<h2><?php echo nl2br(CHtml::encode($data['message'])); ?></h2>
<p>
The above error occurred when the Web server was processing your request.
</p>
<p>
If you think this is a server error, please contact <?php echo $data['admin']; ?>.
</p>
<p>
Thank you.
</p>
<div class="version">
<?php echo date('Y-m-d H:i:s',$data['time']) .' '. $data['version']; ?>
</div>
</body>
You will have access to following attributes in your $data array
code: the HTTP status code (e.g. 403, 500);
type: the error type (e.g. CHttpException, PHP Error);
message: the error message;
file: the name of the PHP script file where the error occurs;
line: the line number of the code where the error occurs;
trace: the call stack of the error;
source: the context source code where the error occurs.
The alternative technique is how you created an action in SiteController,however to activate this you need to change your main config file and route errors to this action:
return array(
......
'components'=>array(
'errorHandler'=>array(
'errorAction'=>'site/error',
),
),
);
If you wish to only skin your errors then this is not necessary, if you want to do more complex stuff like on error log to a DB, send a email to the admin etc then it is good to have your own action with additional logic.

Related

x-editable render html error response

We have a system where customer information is editable in-line.
When someone puts in an email that already exists, I want to return the error message:
Email already exists. <a href='/find-duplicates/id'>Click here to find possible duplicates of this customer</a>
I would like the user to be able to click on the link when s/he sees the error message. The error message is very easy to send; it's rendering the html that's the problem.
Trying to display same kind of link in x-editable field error as #iateadonut.
For anyone wanting to display html in x-editable errors, assuming you have the error with html sent back from server with response status code different from 500 (400 maybe) try :
$(function() {
$('#your_field_id').editable({
error: function(response, newValue) {
if(response.status === 500) {
return 'Service unavailable. Please try later.';
} else {
var error = $.parseHTML( response.responseText )
$(".editable-error-block").html(error)
}
},
});
})
Mostly html parsing response error and injecting it inside x-editable error block.
Found in x-editable doc, options.

Yii2 - check if the user is logged in view

I am trying to check is the user logged inside my view file, but I keep getting this error:
Call to undefined method Yii::app()
I tried adding $ before app but the error is still there (this time it is Undefined variable: app). Is it possible to this is view?
This is the code I use the check if the user is logged:
<?php
if(Yii::app()->isGuest)
echo 'User is not logged!';
?>
In Yii2 the correct syntax is
Yii::$app->user->getIsGuest();
or
Yii::$app->user->isGuest;
Look at the documentation for more details: http://www.yiiframework.com/doc-2.0/yii-web-user.html
Hope it helps.
In yii2 you have to define the app() with $ sign as $app().
<?php
if(Yii::$app->user->isGuest){
echo 'User is not logged!';
}
?>

Yii: Same route but only GET works after confirming there's no controller filter

My urlManager rules: (basically the one comes default)
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',`
My controller:
class SiteController extends Controller {
public function actionSubscribe() {
echo 'gdg';
die();
}
}
My view:
<form method="POST" action="<?php echo $this->createUrl('site/subscribe'); ?>" style="display: inline;">
<input style="margin: 0 18px 0 6px;" type="text" value="e-mail"/>
</form>
When I access it using the url http://localhost/site/subscribe directly it works, but when I type something in the text field and push my enter button to post the form it says The system is unable to find the requested action "error".
I'm very certain that it has something to do with my form. I have so far no problem using active form but for this form I don't have a model and I don't want to use form builder. Any help?
"When I access it using the url http://*/site/subscribe directly it works"
=> You execute subscribe action with GET method.
"but when I type something in the text field and push my enter button to post the form it says The system is unable to find the requested action "error"."
=> You execute subscribe action with POST method.
=> There are some errors and I think Yii try to handle error with your default configure:
return array(
......
'components'=>array(
'errorHandler'=>array(
'errorAction'=>'site/error',
),
),
);
However, Yii can't find error action with your SiteController so it throws The system is unable to find the requested action "error". You can add error action to see information about errors like:
public function actionError()
{
if($error=Yii::app()->errorHandler->error){
$this->render('error', $error);
}
}
More info: The above error variable is an array with the following fields:
code: the HTTP status code (e.g. 403, 500);
type: the error type (e.g. CHttpException, PHP Error);
message: the error message;
file: the name of the PHP script file where the error occurs;
line: the line number of the code where the error occurs;
trace: the call stack of the error;
source: the context source code where the error occurs.

How to control where YII flashes are placed?

With YII Flashes you can do this:
Yii::app()->user->setFlash('success', "Data saved!");
However, when the flash appears on the page, it appears like so:
<div id="yw1">
<div class="alert in alert-block fade alert-info"><a class="close" data-dismiss="alert">×</a>
MESSAGE
</div>
</div>
How do I control WHERE this is placed? I don't see anything in layouts/main.php that controls this, yet they display?
They won't display anywhere unless you are calling them, a good example in the Yii default app is the site/contact view:
<?php if(Yii::app()->user->hasFlash('contact')): ?>
<div class="flash-success">
<?php echo Yii::app()->user->getFlash('contact'); ?>
</div>
<?php else: ?>
...
And the flash is set using the setFlash method
Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
A possibility is that there may be a check in the Controller root class (protected/components/Controller.php), where someone may have written an init() function that checks for them. This would be called on every controller/action call so it may be there.
Another possibility is that whoever created the project edited the flash method in the framework folder (or possibly extended it), you could have a look there, it is in framework/web/auth/CWebUser.php
But as Mik said, try doing a text search in your project for getFlash or even just flash
I would do a textsearch on user->getFlashes() in your views folder. You can output the messages at any place with this
<?php
foreach(Yii::app()->user->getFlashes() as $key => $message) {
echo '<div class="flash-' . $key . '">' . $message . "</div>\n";
}
?>
You, the developer, need to display flash-messages. Yii does not do that for you.
You display them in a view, as shown by Eskimo and Mik, at a location of your own choosing . Yii could never do that, because Yii does not know the layout of your view.

I am unable to upload file to my local host folder in php

I have the follwing code
<form enctype="multipart/form-data" action="upload.php" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>
<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
?>
my page is in http://local.host/nausal/upload.php
now I am having the follwing error though i have created a folder in site with name upload.
Notice: Undefined index: uploaded in C:\wamp\www\Nausal\upload.php on line 15
Notice: Undefined index: uploaded in C:\wamp\www\Nausal\upload.php on line 17
Sorry, there was a problem uploading your file.
If the form is a part of upload.php too, you need to encapsulate the PHP-code and first check if $_FILES is not empty, otherwise you will always get a Notice if you only want to display the form.
<?php
if(!empty($_FILES))
{
//your PHP-code here
}
?>
<!-- your form here -->
Khan sb Place this code in file
called upload.php
create a folder called upload where
you created upload.php
change echo "The file ". basename(
$_FILES['uploadedfile']['name']). "
has been uploaded"; to echo "The
file ". basename(
$_FILES['uploaded']['name']). " has
been uploaded";
This will upload file successfully. First time when upload.php is uploaded there is no file selected so you will see some errors. But after you select a file and click upload you will not see any error. To avoid this error do as suggested by Dr. Molle