I'm trying to make a server laravel, the idea is that given an email address and a password, you can search elusuario in the database and if there's one answer me with json. My problem is I do not know if I'm doing well
My code:
Model
Class Usuario extends Eloquent{
protected $table = 'Usuario';
}
UsuarioController
class UsuarioController extends BaseController{
public function buscarUsuario($Correo, $Contraseña){
$usuario = Usuario::find($Correo,$Contraseña)->toJson();
return View::make('usuario.lista', array('usuario' => $usuario));
}
}
lista.blade.php(view/usuario)
{{$usuario}}
routes.php
Route::get('usuario/{Correo}/{Contraseña}', array('uses' => 'UsuarioController@buscarUsuario'));
I also would like to know, how can I send values ls URL to see if the JSON works well?
If you just need to return a json as response:
class UsuarioController extends BaseController {
public function buscarUsuario($Correo, $Contraseña)
{
$usuario = Usuario::find($Correo,$Contraseña);
return Response::json(array('usuario' => $usuario));
}
}
If you follow the url using your browser:
http://localhost/marsur-servidor/usuario/user@domain.com/mypassword12345
If, of course, your urls start with
http://localhost/marsur-servidor/
And you should see:
{
usuario: {
...
}
}
I'm not sure if Laravel and PHP will play nice with accents too, so you may have to change (in all of your code)
Contraseña
To
Contrasena
If you want to return a JSON output, you shouldn't use View, but the special JSON Response. Otherwise the headers will not be json headers. I am not entirely sure what your question is, but I hope this link helps you out.
<?php
public function buscarUsuario($Correo, $Contraseña){
$usuario = Usuario::find($Correo,$Contraseña)->toJson();
return Response::json($usario);
}
Here's one example of responding with JSON. You don't really need a view to send JSON, you just need a response:
public function buscarUsuario($Correo, $Contraseña) {
$usuario = Usuario::find($Correo,$Contraseña)->toJson();
$array = [ 'field1' => $usario->field1, 'field2' => $usario->field2 ];
$statusCode = 200;
$response = Response::json($array, $statusCode);
return $response;
}
You can, however, leverage some packages out there that will let you transform your JSON whenever it is requested. This is also a great article by Phil Sturgeon that talks about transformers and how they can help you, and has examples on using the package I linked to.
Returning the model directly from the controller will convert it to JSON, and send it as a JSON response:
public function buscarUsuario($Correo, $Contraseña)
{
$user = Usuario::whereEmail($Correo)->first();
if ( $user && Hash::check($Contraseña, $user->password) )
{
return $user;
}
}
This assumes that your database columns are name email
& password
respectively. If they're different, switch them out.
Also, I'm assuming that the passwords in your database are hashed. If they're not (why???), just add the password as another where
query:
public function buscarUsuario($Correo, $Contraseña)
{
return Usuario::whereEmail($Correo)->wherePassword($Contraseña)->first();
}