Connect Flask Restless API to React (Admin-on-rest)

0

I'm learning to work with flask and react to create a rest API (with flask-restless) and a Client (with React, Admin-on-rest). I've already created Api in Flask with Reastless.

I am looking for some advice to connect the API to the client, but I can not find what I need, if you know of any phishing or have some tricks, please tell me, it will be very useful.

    
asked by anonymous 20.06.2017 / 12:29

1 answer

0

See if this helps friend

<?php
    # Definindo pacotes de retorno em padrão JSON...
    header('Content-Type: application/json;charset=utf-8');

    # Carregando o framework Slim...
    require 'Slim/Slim.php';
    \Slim\Slim::registerAutoloader();

    # Iniciando o objeto de manipulação da API SlimFramework
    $app = new \Slim\Slim();
    $app->response()->header('Content-Type', 'application/json;charset=utf-8');

    # Função de teste de funcionamento da API...
    $app->get('/', function () {
        echo "Bem-vindo a API do Sistema de Clientes";
    });

    # Função para obter dados da tabela 'cliente'...
    $app->get('/clientes',function(){

        # Variável que irá ser o retorno (pacote JSON)...
        $retorno = array();

        # Abrir conexão com banco de dados...
        $conexao = new MySQLi("SERVIDOR","USUARIO_SERVIDOR","SENHA_DO_USUARIO","BANCO_DE_DADOS");

        # Validar se houve conexão...
        if(!$conexao){ echo "Não foi possível se conectar ao banco de dados"; exit;}

        # Selecionar todos os cadastros da tabela 'cliente'...
        $registros = $conexao->query("select * from cliente");

        # Transformando resultset em array, caso ache registros...
        if($registros->num_rows>0){
            while($cliente = $registros->fetch_array(MYSQL_BOTH)) {
                $registro = array(
                            "CODIGO"   => $cliente["CODIGO"],
                            "NOME"     => utf8_encode($cliente["NOME"]),
                            "TELEFONE" => $cliente["TELEFONE"],
                            "EMAIL"    => $cliente["EMAIL"],
                        );
                $retorno[] = $registro;
            }
        }

        # Encerrar conexão...
        $conexao->close();

        # Retornando o pacote (JSON)...
        $retorno = json_encode($retorno);
        echo $retorno;

    });

    # Executar a API (deixá-la acessível)...
    $app->run();
    ?>

In time, for the full operation of these methods, we need to create and include one more file in the api folder. So, create a new file in Notepad ++, save it as .htaccess, and insert the following source code:

RewriteEngine On

# Some hosts may require you to use the 'RewriteBase' directive.
# If you need to use the 'RewriteBase' directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
    
20.06.2017 / 12:38