Error handling custom component Joomla - joomla

How to set and retrieve error inside component in model I have:
if (empty($coupon->coupon_id))
{
$this->setError( JText::_( "Invalid Coupon" ) );
return false;
}
How to retrieve this error inside controller? this->getError gives nothing :(
Thanks

Do you want to return the error to the screen?
If so, http://docs.joomla.org/Display_error_messages_and_notices

In your controller you need to invoke your model and use the $model->getError(); function instead of $this->getError();

Related

How do i redirect to a page from php yii2.0 components?

I Have a component ,in which have one init() function which is to be called before each action.
I only need to check if the session is expired redirect the user to login.
Here is my code,
config/main.php coponents -
'MyGlobalClass'=>[
'class'=>'common\components\MyGlobalClass'
],
and
'bootstrap' => ['log','MyGlobalClass'],
components/MyGlobalClass .php :
class MyGlobalClass extends \yii\base\Component{
public function init() {
//echo "Hi";
if(Yii::$app->user->isGuest)
{
//echo 'called';exit;
Yii::$app->getResponse()->redirect(Yii::$app->urlManager->createUrl('login'));
}
//parent::init();
}
}
so it is coming inside if{} but not redirecting to login page.
You can try this, it worked for me:
return Yii::$app->getResponse()->redirect('admin/page/signin');
You may use this to send your response
Yii::$app->response->send();
but please review the guide for controller/action distinct access control
http://www.yiiframework.com/doc-2.0/guide-security-authorization.html
You can try the code below:
Yii::$app->getResponse()->redirect(Yii::$app->urlManager->createUrl('login'));
return;
correct way is :
Yii::$app->response->redirect(Yii::$app->urlManager->createAbsoluteUrl('site/logout'));
return;
This way you'll get an infinite redirect loop: The component is called on each controller call, redirection will never stop.
Better to manage it from public function behaviors() on each controller, check the Authorization section on Yii2 documentation.
May be this one help you :)
return Yii::$app->getResponse()->redirect(['user/login']);
or
return Yii::$app->getResponse()->redirect(Yii::$app->urlManager->createUrl('user/login'));

Laravel 4 route-model binding exceptions doesn't work despite docs and examples

I read a lot about Laravel4 Route-model binding (L4 docs, tutorials, etc.) but still exceptions (i.e. the model is not found) don't work for me
These are my basic files
routes.php:
Route::model('game', 'Game', function(){
// override default 404 behavior if model not found, see Laravel docs
return Redirect::to('/games');
});
...
Route::get('/games/edit/{game}', 'GamesController#edit');
GamesController.php
class GamesController extends BaseController {
...
public function edit(Game $game){
return View::make('/games/edit', compact('game'));
}
}
Pretty straight, but I get this error: Argument 1 passed to GamesController::edit() must be an instance of Game, instance of Illuminate\Http\RedirectResponse given
If I type http://mysite.dev/games/edit/1 all is fine (model with ID = 1 exists)
If I type http://mysite.dev/games/edit/12345 (no model with that ID) the ugly error above is triggered instead of the redirect I specified
I also looked at this (the bottom part where a Redirect closure is suggested: that is just what I am doing!) but no way to make it work: laravel 4 handle not found in Route::model
What's wrong with it? Please any help?
Thanks in advance
In Route::model you declare which variable will be a model instance, you shouldn't use it to do a redirection that way. Instead of that, specify that $game is of type Game and then work with your routes:
Route::model('game', 'Game');
...
Route::get('/games/edit/{game}', 'GamesController#edit');
Then if you access to /games/edit/3 GamesController::edit will receive an instance of Game class whose id=3
I ended up by setting a general "Not Found" error catcher, like this:
// routes.php
App::error(function(Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e) {
return Response::make('Not Found', 404);
});
...
Route::model('game', 'Game');
...
Route::get('/games/edit/{game}', 'GamesController#edit');
What I understand is that if I want a custom redirect and not a general 404 page (i.e. take the user to games' list if model not found), I CAN'T use the route-model-binding
In other words, I have to use Route::get('/games/edit/{id}', 'GamesController#edit'); and then do my application logic inside the 'edit' method:
public function edit($id){
$game = Game::findOrFail($id);
// if fails then redirect to custom page, else go on saving
}
I'm very new to Laravel, but as far as I can see this has nothing to do with the closure, but with the use of "Redirect::to" inside that closure. Using "App::abort( 404 );" works.

Laravel 4: redirect if item doesn't exists, ModelNotFoundException doesn't work anyway I try it

I'm following Dayle Rees' book "Code Bright" tutorial on building a basic app with Laravel (Playstation Game Collection).
So far so good, the app is working but, following his advices at the end of the chapter, I'm doing my homeworks trying to improve it
So, this snippet is working fine for existing models but throws an error if the item doesn't exists:
public function edit(Game $game){
return View::make('/games/edit', compact('game'));
}
In other words, http://laravel/games/edit/1 shows the item with ID = 1, but http://laravel/games/edit/21456 throws an error since there's no item with that ID
Let's improve this behaviour, adapting some scripts found also here on StackOverflow (Laravel 4: using controller to redirect page if post does not exist - tried but failed so far):
use Illuminate\Database\Eloquent\ModelNotFoundException; // top of the page
...
public function edit(Game $game){
try {
$current = Game::findOrFail($game->id);
return View::make('/games/edit', compact('game'));
} catch(ModelNotFoundException $e) {
return Redirect::action('GamesController#index');
}
}
Well... nothing happens! I still have the error with no redirect to the action 'GamesController#index'... and please notice that I have no namespaces in my Controller
I tried almost anything:
Replace catch(ModelNotFoundException $e) with catch(Illuminate\Database\Eloquent\ModelNotFoundException $e): no way
put use Illuminate\Database\Eloquent\ModelNotFoundException; in Model instead of Controller
Return a simple return 'fail'; instead of return Redirect::action('GamesController#index'); to see if the problem lies there
Put almost everywhere this snippet suggested in Laravel documentation
App::error(function(ModelNotFoundException $e)
{
return Response::make('Not Found', 404);
});
Well, simply nothing happened: my error is still there
Wanna see it? Here are the first two items in the errors stack:
http://www.iwstudio.it/laravelerrors/01.png
http://www.iwstudio.it/laravelerrors/02.png
Please, can someone tell me what am I missing? This is driving me mad...
Thanks in advance!
Here are few of my solutions:
First Solution
The most straightforward fix to your problem will be to use ->find() instead of ->findOrFail().
public function edit(Game $game){
// Using find will return NULL if not found instead of throwing exception
$current = Game::find($game->id);
// If NOT NULL, show view, ELSE Redirect away
return $current ? View::make('/games/edit', compact('game')) : Redirect::action('GamesController#index');
}
Second solution
As I notice you may have been using model binding to your route, according to Laravel Route model binding:
Note: If a matching model instance is not found in the database, a 404 error will be thrown.
So somewhere where you define the model binding, you can add your closure to handle the error:
Route::model('game', 'Game', function()
{
return Redirect::action('GamesController#index');
});
Third Solution
In your screenshot, your App::error seems to work as the error says HttpNotFound Exception which is Laravel's way of saying 404 error. So the last solution is to write your redirect there, though this apply globally (so highly discouraged).

Checking for CodeIgniter session_id to not be set

I'm trying to check if its set in my codeigniter application like:
elseif(!isset($this->session->userdata('session_id'))){ ## SESSION data is NOT set
// load a generic 'issue' occurred view.
}
but I keep getting this error, is !isset not valid in Codeigniter? How do I go about easily checking this?
Thanks
You must use the code like below:
$id = $this->session->userdata('session_id');
// your other code
elseif(!isset($id)){
// load a generic 'issue' occurred view.
}
The reason you can see here
You coud also do this ,
elseif($this->session->userdata('session_id') !== false)
{
// SESSION data is NOT set
// load a generic 'issue' occurred view.
}
Note: The function returns FALSE (boolean) if the item you are trying to access does not exist.

How can I debug $Model after validation?

I want to see the content of validationErrors => array(???) of the $Model after a failed validation, but there is no "afterValidation()" method.
Does anyone know how can I see that or at least how would it look exactely?
Thank's!
On Controller, you can validate data before you trying save:
$this->ModelName->set($this->request->data);
if ($this->ModelName->validates()) {
// success
} else {
// failed
$errors = $this->ModelName->validationErrors;
}
Reference:
Validating Data from the Controller
Use $this->ModelName->invalidFields() after you have made the save/whatever you're doing:
For example:
debug($this->ModelName->invalidFields());
If you have a redirect at some point after that call, you might not see the data in your view. In this case, you can always do die(); either right after or wrapped around your call like so:
die(debug($this->ModelName->invalidFields());

Resources