drelgkxl93433
2014-04-09 14:31
浏览 74
已采纳

如何从laravel中的控制器发送json到视图?

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?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试制作服务器laravel,这个想法是给定一个电子邮件地址和一个密码,你可以 在数据库中搜索elusuario,如果有人用json回答我。 我的问题是我不知道我是否做得很好

我的代码:

模型

 类Usuario扩展Eloquent {
protected $ table ='Usuario'; 
} 
   
 
 

UsuarioController

 类UsuarioController扩展BaseController {
 
公共函数buscarUsuario($ Correo,$Contraseña){
 $ usuario = Usuario :: find($ Correo,$Contraseña)  ) - > toJson(); 
 
返回View :: make('usuario.lista',array('usuario'=> $ usuario)); 
} 
 
} 
   
 
 

<强> lista.blade.php(查看/ usuario)

 <代码> {{$ usuario}} \  ñ  
 
 

<强> routes.php文件

 <代码>路线::得到('usuario / {邮报 } / {Contraseña}',array('uses'=&gt;'UsuarioController @ buscarUsuario')); 
   
 
 

我也想知道,我该怎么办? 发送值ls URL以查看JSON是否运行良好?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • ds3422222 2014-04-09 14:38
    已采纳

    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
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doubingguan3425 2014-04-09 14:38

    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);
    }
    
    评论
    解决 无用
    打赏 举报
  • dongqiao3927 2014-04-09 14:38

    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.

    评论
    解决 无用
    打赏 举报
  • dsomm80482 2014-04-09 14:47

    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();
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题