I'm trying to do a redirect from my service class. I don't want to return the url to the caller because it's not being accessed directly from controller and it should return other data in some cases.
However I found that i can do the redirect this way:
Redirect::away('http://someexternalurl.com')->send();
This seems to work fine - the user gets redirected to the url I entered. The problem is that in this case the app "lives on" so following commands get executed. That's not what I need.
If I die(); immediately after that my session changes don't seem to be "saved".
Is there a way to make a redirect and stop the App immediately after that without simply "killing" it?
Basically is there a way to rewrite this code
Session::forget('myval');
Redirect::away('http://someexternalurl.com')->send();
mail('my#mail.com', 'Test', 'Still running');
So that myval would be gone from the session, the user would be redirected to the url and mail would not be sent (actually anything after the redirect shouldn't be executed)?
Thank you
The solution is to throw an Exception and adapt the render method of the Exception Handler.
In Your Controller:
// app/Controllers/DashboardController.php
public function index()
{
\App\Services\Test::test();
return view('dashboard');
}
My example service :
// app/Services/Test.php
<?php
namespace App\Services;
class Test {
public static function test() {
throw new \App\Exceptions\TestException('test');
}
}
In your Exception Handler :
// app/Exceptions/Handler.php
public function render($request, Exception $exception)
{
if ($exception instanceof TestException) {
return redirect(url('/?error=' . $exception->getMessage()));
}
return parent::render($request, $exception);
}
My example Exception (you can name it whatever you want), it just needs to extend the default Exception.
<?php
namespace App\Exceptions;
class TestException extends \Exception {
}
It looks like you just need to return on you Redirect?
return Redirect::away('http://someexternalurl.com')->send();
Here's the solution for Laravel 5.2. I reckon it would be work at all 5.*
abort(301, '', ['Location' => 'http://antondukhanin.ru']);
methodThatWouldntBeExecuted();
the function throws HttpException, so that will interrupt execution of any next commands
Related
I am trying to implement exception handling in my application. For this Laravel framework has its own mechanism to handle the exception using report and render method. But to implement exception I need to track the source from where the exception has been raised e.g. specific page, route etc. For this I need to pass the url to report and render method but unable to do so. What needs to be done in order to implement this in below report and render function.
public function report(Exception $e)
{
parent::report($e);
}
public function render($request, Exception $e)
{
/* Token mismatch Exception handler start */
if ($e instanceof \Illuminate\Session\TokenMismatchException) {
return response()->view('errors.sessionExpire', [], 500);
}
/* Token mismatch Exception handler start */
return parent::render($request, $e);
}
As you can see from your own example, you have an instance of Request in the argument list. And Request has all request-specific details like current route, URL and so on.
$request->url(); // Current request URL
$request->fullUrl(); // With query parameters
$request->route(); // Get the route closure for this request path
You can also create your own exception classes that accept as many parameters as you wish!
And the less comfortable way already mentioned – you could go through the exception trace.
You need to use Exception::getTrace
var_dump($e->getTrace());
above line will give you all details regarding exception.
public function report(Exception $e){
echo '<pre>'; // add this line
print_r($e->getTrace()); // add this line
parent::report($e);
}
I just install the Errbit in my PC using ruby, and it's already running on my local. And I developed a web apps using Laravel 4, but I didn't know how can I send all of the errors of my web apps to the errbit.I already installed this package, but I didn't know how to implement this on my web apps.
I already tried to use below script on my ../app/start/global.php on Applicatoin Error Handler, but it's only show me the white page. This is my script:
<?php
...
App::error(function(Exception $exception, $code)
{
Errbit::instance()->notify($exception);
Log::error($exception);
});
...
And this is how I create an error in my controller, I remove the semicolon delimeter like this:
<?php
class someController extends \BaseController {
public function index(){
return View::make('editor.index') //Remove ';' at the end of line code
}
}
If I remove the errbit instance Errbit::instance()->notify($exception); on global.php, the page will return Whoops error from laravel, but if I add errbit instance, it's only show the blank white page, and nothing happend to the errbit report.
And also I already tried to put the errbit on the method of controller like below, but I still get nothing.
<?php
public function index(){
try {
return View::make('editor.index')
} catch (Exception $e) {
Errbit::instance()->notify($e);
}
}
Please help me, and thanks before.
I am new in zendframework. I am using apigility for rest services and ApiProblemListner to return the response if any error occures.
I have one function in model and this function just through an exception using php exception to use in catch block
I am using model function as the utility function in controller to catch those Exception. While catching exception I am using as
try{
imageUploade(); // this function in model and throwing exception if any error
}catch(\Exception $e){
return new ApiProblemResponse(new ApiProblem(500 , $e->getMessage()));
}
if imageUploade() throw an exception if the image size is more then I am able to catch the exception in catch block. I tried echo $e->getMessage(); and its printing the exception bt if I use new ApiProblem(500 , $e->getMessage()) it is not retuning the json error response with the 500 message. It is returning nothing. even it is not showing any error.
Seems like it is unable to render the error with this class. I am not sure if any event needs to add.
I have tried to search for documents but unable to find it.
Thanks in advance.
Normally it should work if you return an ApiProblemResponse straight from a controller action.
Are you sure your Api-Problem module is active?
Try once like this:
<?php
Application\Namespace;
use ZF\ApiProblem\ApiProblem;
use ZF\ApiProblem\ApiProblemResponse;
class IndexController
{
/**
* #return ApiProblemResponse
*/
public function indexAction()
{
return new ApiProblemResponse(new ApiProblem(500, 'test'));
}
}
If that doesn't work then I think your Api-Problem module is not running or the exception is never caught.
I have a query with try-catch block. I am going to create object and if it fails it will throw error message or say redirect on some other page.
Code
function __construct() {
try{
$this->ServiceObj = AnyClass::getService('XXX');
}
catch{
return Redirect::to('/');
}
}
public function MyOwnFunction() {
$getValueofCode = $this->_ServiceObj->set($endPoint_Url); //it's only example method not want to set anything
}
Is this correct way to define and use the try catch block in a constructor? Can I use try & catch block in construction? If NO than is there a better way to achieve this?
Thanks in Advance!!!
You can definitely use try catch in a constructor, just don't forget to specify the exception class you want to catch like catch (\Exception $e). However Laravel will not process the returned redirect. (By definition constructors shouldn't even return something)
An easy fix for this would be calling send() on the redirect. This will return it immediately to the client and stop the app.
try{
$this->ServiceObj = AnyClass::getService('XXX');
}
catch(\Exception $e){
Redirect::to('/')->send();
}
But since you mentioned a login page, you might be better of using a before filter to check and redirect. Like the built in auth filter does.
A third solution would be App::error. In app/start/global.php you could do something like this:
App::error(function(FooBarException $exception)
{
return Redirect::to('/');
});
Of course this only works if your exception is specific enough (replace FooBarException with the exception you want to catch)
I'm currently using Laravel and I'd like to be able to extend the withError call to log it everytime it's called. I know it goes through the RedirectResponse object in this call:
public function __call($method, $parameters)
{
if (starts_with($method, 'with'))
{
return $this->with(snake_case(substr($method, 4)), $parameters[0]);
}
throw new \BadMethodCallException("Method [$method] does not exist on Redirect.");
}
however this is part of the Illuminate (Laravel) vendor code and I Don't want to modify it in case I Want to update Laravel later. Is there a better way to do what I'm trying to do?
This should work.
App::error(function(Exception $exception)
{
Log::error($exception);
});
References:
http://laravel.com/docs/errors
http://fideloper.com/laravel4-error-handling