Call PHP function via Link using MVC

Asked

Viewed 170 times

0

I’m having Difficulty/Problem requesting a function in PHP:
The function below is located in a class inside the Folder lib.
Language.php

<?php

namespace app\lib;

class Language {

    public function setLingCookie(){
        if(isset($linguagem)){
            setcookie("IDIOMA", $linguagem, time()+3600, "/", "localhost/");
            echo 1;
        }else{
            echo 0;
        }
    }
    .
    .
    .
}


My difficulty is to trigger this function on my page, and I will trigger it from a Link.

Leaving registered here that I have tried some functions by Ajax but I was not successful because I am not able to handle the request or I am not able to send.

Edit: I’m using the Composer only for the Autoload.

1 answer

1

You need a page, for example, setcookie.php, and on this page, you should instantiate the class you created, more or less like this:

$l = new Language();
$l->setLingCookie();

Are you using a framework? Depending on the framework, there may be ways to access this function by routes, but since you didn’t mention any, the method would be by a php file that instantiated this class you made

I hope I’ve helped!!

  • So, I’m using Composer only for Autoload. I can create a Cookie by Js nothing complicated but wanted to do all structure by PHP. I thought about what you told me by passing a parameter but it would have to be a POST in this new file through Ajax right? It would also be possible for me to click on the link for that route by passing a parameter and a redirect back. Well, thank you very much for the Help!

  • That, the way would be that same! You’re welcome!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.