0

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?

4 Answers 4

1

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/[email protected]/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
Sign up to request clarification or add additional context in comments.

8 Comments

How I can know if it works?, should send data into URL to test?
If you are opening it in your browser, you should see a json.
Edited to provide info about user and password.
Yes, that should work fine. Just added an observation about ñ in my answer.
mu route is fine?? Route::get('usuario/{Correo}/{Contrasena}', array('uses' => 'UsuarioController@buscarUsuario'));
|
1

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.

8 Comments

How I can know if it works?, should send data into URL to test?
Use REST client and send requests to your app. Here's a chrome extension that works pretty good: chrome.google.com/webstore/detail/postman-rest-client/…
How I can get the response in the view?
There is no 'view'. You're simply returning a JSON Response. If you're trying to get to this json data from another view, then you need to issue an AJAX request to this route and then receive the JSON through javascript.
doing ajax calls? Sure are.. this one uses jquery: code.tutsplus.com/tutorials/…
|
0

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);
}

1 Comment

How I can know if it works?, should send data into URL to test?
0

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.