Call PHP files and functions through AngularJS

1

Before delving deeper into the question, I would like to clarify that I already have a method that works. But since I do not have so much knowledge in backend , more precisely in PHP , I do not know how "correct" my code is by analyzing security issues, scalability, and best practices. I also do not use any framework in backend .

The "problem"

After doing some research (not related to this specific problem), I came across some examples of users who were calling functions in PHP (from AngularJS ) differently than I currently do. Actually, I did not find any similar example to what I do, so I had this doubt, because I could be doing "wrong".

The method I do would be this:

AngularJs

var url  = '/php/produtos.php?action=carregaProduto';
return $http.get(url).then(
    function(response) {return response.data;}
)

PHP - products.php

switch($_GET['action']) {
    case 'carregaCategoria': carregaCategoria();
    break;

    case 'carregaProduto': carregaProduto();
    break;

    case 'adicionaProduto': adicionaProduto();
    break;
}

function carregaCategoria() {
    //Executa a função
}
function carregaProduto() {
    //Executa a função
}
function adicionaProduto() {
    //Executa a função
}

I have some php files segmented by "categories", for example produtos.php , clientes.php , filtros.php , etc. For each file I follow the same pattern, defining a case to call the function.

The models

The models that I observed of tutorials have a slightly different structure, with the call of the function directly in url , however without action or without the definition of the function itself in url . They also use a PHP structure other than the one I use, with the definition of class and Public function .

For example:

AngularJs

var url = '/produtos/carregaProduto';
return $http.get(url).then( 
    function(response) {return response.data;}
)

PHP - products.php

class listaProduto {
    public function carregaCategoria() {
        //Executa a função
    }
    public function carregaProduto() {
        //Executa a função
    }
    public function adicionaProduto() {
        //Executa a função
    }
}

As you can see, the call made in AngularJs mentions the file, but without extension and function it is also defined, but without () .

Reading a little bit about it, I noticed that a "Routes" setting in% with% would be required for this to work. This determines the pattern of routes and methods to be called.

My question

With this, my question would be about the "correct" or better method of obtaining the final result, which is to get data from the database, or to execute some process in backend .

Is the method I use currently wrong? Is the method shown in the example correct? Why?

Remembering that I want to take a good look at the points I mentioned at the beginning of the issue (security, scalability and good practices). In the example I used few functions, but in a larger application, such as an e-commerce, I will eventually have hundreds of functions and several files.

    
asked by anonymous 28.09.2016 / 15:32

0 answers