Laravel : route not hitting controller - debugging

Route::any('mobileLogin', ['as' => 'mobileLogin', 'uses' =>
'App\Controllers\LoginController#mobileLogin']);
For some reason my perfectly working route has stopped working and a "whoops" error is being shown using app/views/error-page.blade.php
Here is the controller function:
public function mobileLogin()
{
try
{
Log::info('----------------');
Log::info('Inside mobileLogin');
}
catch (\Exception $e)
{
Log::info('*********** ERROR**************');
Log::info('Message: ' . $e->getMessage());
Log::info('Trace String : ' . $e->getTraceAsString());
Log::info("*********** END of ERROR *************");
}
}
The /app/storage/logs/laravel.log don't show any error, hence trying to find what exactly went wrong.
In my config/app.php debug is set to true as well.
I've put log messages in my controller function as well, but it's failing before it even reaches there !
How does one debug this ?
If Laravel framework isn't routing to the controller itself, then where can I dig for the issue ?

Found the issue - it was an error in one of the php files itself.
I expected for a syntax error an error would show in the laravel.log, but no !
There is another error.log at the base of the app, which luckily had it !

Related

Laravel 5.4 - Adldap - Call to undefined method Adldap\Adldap::search()

I have an issue with my Laravel installation and the use of Adldap...
The error message I receive :
FatalThrowableError in UserCreationController.php line 100:
Class 'App\Http\Controllers\Adldap' not found
I have installed/deployed Adldap according to the documentation and it is working when I call it from some other location.
Working stuff :
Route::get('ldap', function() {
$results = Adldap::search()->where('ou', 'ends_with', ' Users')
->orWhere('ou','not_contains', 'Production')
->sortBy('ou', 'asc')
->get();
foreach ($results as $result) {
dump ($result->ou);
}
The page displays the dump correctly. All is fine.
Not working stuff (yields error code listed above).
Route calling a Controller...
Route :
Route::get('newuser', 'UserCreationController#GetUserOrganizationalUnits');
Controller :
public function GetUserOrganizationalUnits()
{
$results = Adldap::search()->where('ou', 'ends_with', ' Users')
->orWhere('ou','not_contains', 'Production')
->sortBy('ou', 'asc')
->get();
return view('newuserform',compact('results'));
}
Why is it working from the web php with the routes directly but not from the called Controller?
I already try adding explicitely the following as well...
use Adldap\Contracts\AdldapInterface;
The facade is declared and it works in the web routes without even calling this...
Can you please help ?
Thanks.
I think you forgot to include the Facade
Add: use Adldap; in your UserCreationController.php
You'll also need to have this in your UserCreationController to get this working with the "use Adldap\Contracts\AdldapInterface;" approach:
protected $adldap;
public function __construct(AdldapInterface $adldap)
{
$this->adldap = $adldap;
}
Or implement the facade in your config/app.php:
'Adldap' => Adldap\Laravel\Facades\Adldap::class

Laravel default route to 404 page

I'm using Laravel 4 framework and I've defined a whole bunch of routes, now I wonder for all the undefined urls, how to route them to 404 page?
In Laravel 5.2. Do nothing just create a file name 404.blade.php in the errors folder , it will detect 404 exception automatically.
Undefined routes fires the Symfony\Component\HttpKernel\Exception\NotFoundHttpException exception which you can handle in the app/start/global.php using the App::error() method like this:
/**
* 404 Errors
*/
App::error(function(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $exception, $code)
{
// handle the exception and show view or redirect to a diff route
return View::make('errors.404');
});
The recommended method for handling errors can be found in the Laravel docs:
http://laravel.com/docs/4.2/errors#handling-404-errors
Use the App::missing() function in the start/global.php file in the following manner:
App::missing(function($exception)
{
return Response::view('errors.missing', array(), 404);
});
according to the official documentation
you can just add a file in: resources/views/errors/
called 404.blade.php with the information you want to display on a 404 error.
I've upgraded my laravel 4 codebase to Laravel 5, for anyone who cares:
App::missing(function($exception) {...});
is NO LONGER AVAILABLE in Laravel 5, in order to return the 404 view for all non-existent routes, try put the following in app/Http/Kernel.php:
public function handle($request) {
try {
return parent::handle($request);
}
catch (Exception $e) {
echo \View::make('frontend_pages.page_404');
exit;
// throw $e;
}
}

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).

Laravel 4 handling 404 errors

I am new to Laravel and I'm trying to catch any requests that do not match existing routes. Currently I am getting a ...
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
I have created an App::missing() handler in the app/start/global.php as described on the documentation page but that doesn't help. I also tried creating an App::error(function(NotFoundHttpException $exception, $code) handler, but that didn't help either.
Any help would be appreciated.
You need this: In 'app/start/global.php' add:
App::missing(function($exception)
{
return Response::view('errorView', array(), 404);
});
And of course in views create (in this case) errorView.blade.php
EDIT: This method handle all "404 Not Found" errors.
That's likely due to debugging being turned on in the app/config/app.php file. Try turning that value to false and see if your custom handler than 'handles" it.
Additionally, your error handler needs to return a Response in order to "short-circuit" the request and actually respond with the result of your error handler - Are you returning a value / Response class from your App::error() handler? (Please show us that code).
Here's an article on Laravel 4 Error Handling without outlines the process of how Laravel uses App::missing() and App::error() handlers when errors occur.
Pay specific attention to the "meat of the Handler class" section - it outlines how you need to return a value of some sort from the handler in order for it not to pass the Exception to the next handler (Likely your Whoops error output that displays when debug is set to true in app/config/app.php).
Here is how to throw a 404 error when a ModelNotFoundException occurs
App::error(function(Illuminate\Database\Eloquent\ModelNotFoundException $exception, $code)
{
return Response::view('errors.404', array(), 404);
});
in
app -> config -> app.php
if you make
'debug' => true,
to this
'debug' => false,
You will get an user experience environment which is other than the debugging mode. Please make sure you have checked it.
Thanks :)
I was able to make it works using the following code. I let 'debug' => true, in my local configuration.
In app/start/global.php
App::error(function(Symfony\Component\HttpKernel\Exception\NotFoundHttpException $exception)
{
Log::error($exception);
$response = Response::make(View::make('response')
->with('data', null)
->with('meta', 'Error, incorrect path')
->with('pagination', null));
$response->setStatusCode(404);
$response->header('content-type', 'application/json', true);
return $response;
});
If of any interest, the content of my view is
<?php
echo json_encode(array(
'data' => $data,
'pagination' => $pagination,
'meta' => $meta), JSON_PRETTY_PRINT);
?>
The main challenge was to use Symfony\Component\HttpKernel\Exception\NotFoundHttpException as the type of the Exception and not only NotFoundHttpException.
By using NotFoundHttpException, a 500 error was thrown.

Laravel 4 exception handling - same code works in 1 case and not in 2 other cases

I am trying to handle a few exceptions in Laravel 4, and the same snippet of code works for one of them but not for the others.
I declared an Exceptions.php file that I added to my composer.json.
In Exceptions.php, I declared the following:
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Class NotAllowedException extends Exception {};
The following handler, in Exceptions.php, works properly:
// snippet 1
App::error(function(NotAllowedException $e, $code, $needed)
{
return Response::make(View::make('special.error')->with('error', array('headline' => 'Not Allowed',
'description' =>'<p>Your user has not enough privileges to perform the requested operation.</p>')), 401);
});
When I try to do the same to handle HttpNotFoundExceptions and ModelNotFoundExceptions, the following 2 snippets which are copies of the first do not work and yield an Undefined variable: error error.
// snippet 2
App::error(function(ModelNotFoundException $e)
{
return Response::make(View::make('special.error')->with('error', array('headline' =>'Not Found',
'description' => '<p>It seems you have tried to access a page that does not exist.</p>')), 404);
});
// snippet 3
App::error(function(NotFoundHttpException $e)
{
return Response::make(View::make('special.error')->with('error', array('headline' =>'Not Found',
'description' => '<p>It seems you have tried to access a page that does not exist.</p>')), 404);
});
I was only able to make the NotFoundHttpException work by putting this into global.php:
App::missing(function($exception)
{
return Response::make(View::make('special.error')->with('error', array('headline' =>'Not Found',
'description' => '<p>It seems you have tried to access a page that does not exist.</p>')), 404);
});
But I do not know why it works if I put it there and it does not work if I put it in Exceptions.php.
Trying to put snippets 2 and 3 in global.php yield in both case an Internal Server error.
To recap, Questions:
I think I am following the ModelNotFoundException handling as per Laravel4's documentation; what What am I doing wrong / how to make it work?
why does the App::missing work only if I put it in global.php ? I fear that I might be missing something important about Laravel's inner workings.
Thanks in advance to anyone who can shed some light on those 2 issues

Resources