laravel Auth attempt parameters issue - laravel

I have the following code:
if(!empty($email) && !empty($password))
{
if (Auth::attempt(array('email' => $email, 'password' => $password, 'activated' => 1)))
{
return Redirect::route('home');
}else{
return Redirect::route('signin')->with('message', 'Login Failed. Please try again.');
}
}else{
return Redirect::route('error');
}
The above is the login function of my application. The login view loads and user enters date and above is executed. Auth::attempt is passed a third parameter above to check if activated field in table is 1. It throws a weird error and this is it:
Please help me understand what is going wrong.

Your app/model/User.php file is missing a closing bracket }. That is what the debug tool is telling you.

Looks like you're just missing a closing } on your function or class.
The error unexpected end means php finished processing the file but something hasn't been closed (php was expecting more), and because it's expecting a function this means the class hasn't been closed.

Related

Laravel : route not hitting controller

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 !

how can I return the validation errors for a REST call?

I am making a REST api for my application. It almost works, but I want it to return the validation errors when they occurs.
With CakePHP 2.x I see there was an invalidFields method, but it's not there anymore with Cake3. I see, instead, that there is an errors method in the Form class, but I don't know how can I use it.
This is what I have in my Users/json/add.ctp file:
<?php
$echo = [
'errors' => $this->Form->errors(),
'user' => $user
];
echo json_encode($echo);
As I said it doesn't work (I know that probably Form is used out of its scope here, but I don't even know if I am on the right path)...
This is the result:
{
"message": "Missing field name for FormHelper::errors",
"url": "\/fatturazione\/api\/users.json",
"code": 500
}
What is the correct way to display all the form errors when they occour?
This is what I would do, check for validation-errors in your controller and send them to the view serialized
public function add() {
$this->request->allowMethod('post');
$data = $this->Model->newEntity($this->request->data);
if($data->errors()) {
$this->set([
'errors' => $data->errors(),
'_serialize' => ['errors']]);
return;
}
//if no validation errors then save here
}

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

Laravel 4 Email not working

I've been looking for the answer for two days and still nothing. I've followed several tutorials where nothing more than just a few config settings are required, but my Laravel 4 app still doesn't want to send an e-mail. It always throws the same Exception and nobody seems to have the answer. It seems that I've tried everything but the working one.
Route::get('/sendemail', function() {
$data['user'] = 'Test User';
Mail::send('email.test', $data, function($m) {
$m->to('myemail#example.com')->subject('Email test');
});
});
But when I'm in the specified route, it always throws the same error:
Argument 1 passed to Swift_Transport_EsmtpTransport::__construct()
must implement interface Swift_Transport_IoBuffer, none given
The config is ok and even changing the driver from 'smtp' to 'mail' in config/mail.php throws also Exception.
Argument 1 passed to Swift_Transport_MailTransport::__construct() must be an instance of Swift_Transport_MailInvoker, none given
I'm stuck in here and I don't know that to do. My composer is up to date and PHP version is 5.4.4.
Any advice will be much appreciated.
Thank you.
You have probably found an answer by now.
But I think you need a second argument for the to.
Try:
$data = array('user' => $user);
Mail::send('email.test', $data, function($message) use ($user)
{
$message->to($user->email, $user->name)->subject('Email Test');
});

Resources