Return javascript result with controller

Asked

Viewed 1,044 times

2

It is possible within a Controller to return a javascript action and proceed with the controller script ?

Example:

Call to Login page:

<li><a href="javascript:" link="{{ route('login') }}" class="popup">logar</a></li>

$('.popup').click(function(ev){
    var URL = $(this).attr("link");

    window.open(URL,'janela', scrollbars=yes, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
});

Controller

class testeController extends BaseController {

public function teste($process = null)
{
    if ($process) {
        return function();
    }

    echo '<script type="text/javascript">window.close()</script>';

    if(Auth::check()){
        $role = User::find(Auth::user()->id)->roles()->first();
        return Redirect::to($role->name)->with('success', 'Logado com sucesso');
    }else{
        return Redirect::to('/')->with('warning', 'Não foi possível autenticá-lo, tente novamente mais tarde ou nos contate');
    }
...

however javascript echo does not work inside the controller, only if I give a Return, and if I give a Return the rest of the controller function is not executed.

What is the best procedure in this case ?

Tentative1:

class testeController extends BaseController {

public function teste($process = null)
{
    if ($process) {
        return function();
    }

    Functions::jsController('close');

    if(Auth::check()){
        $role = User::find(Auth::user()->id)->roles()->first();
        return Redirect::to($role->name)->with('success', 'Logado com sucesso');
    }else{
        return Redirect::to('/')->with('warning', 'Não foi possível autenticá-lo, tente novamente mais tarde ou nos contate');
    }
...

public static function jsController($acao){
    if($acao == 'close'){
        return '<script type="text/javascript">window.close()</script>';
    }
}
  • The best way to do this is with Ajax.

1 answer

3

Unable to run Javascript from within Laravel Controller.

In this specific case the best procedure seems to be to simply include Javascript code window.close() in the callback of your Ajax call.


All indicates that you have a popup window to login. You want the popup window to close and the browser to be redirected to a specific address.

The side of back-end should be restricted to just checking credentials, and return if there were success or error.

  • If successful, you must also activate the cookie authentication and inform the URL to be redirected. You can use setcookie and return a JSON something like this: { "sucesso": true, "url": "http://exemplo.com/dashboard" }

  • If there was an error, you should preferably also indicate what the error was, returning a JSON more or less like this: { "sucesso": false, "erro": "Senha incorreta." }

With this return of the Controller, your Javascript client-side is that you should close the window and redirect, or show the error message.

Note: this is just one possible approach out of several. (And I’m assuming you have a popup window opened from another page.)


Just adding more information: using jQuery on the client side, there is the practical function $.getScript(url); which makes an Ajax call to the "url" and automatically executes the returned Javascript code. Thus, you can directly return Javascript code to be executed:

class testeController {

    public function teste()
    {
        return 'window.close();'
    }

}

That knowledge is useful, but I don’t think it applies in your case.

  • very good your explanation. I will do the tests and already return.

  • Bruni, your third option gave me a good idea, but that also was not valid. I still could not find a solution. -- Updated theme

  • @tiaguinhow: The solution is not there. You will never be able to redirect the window that opened the popup from the controller. You cannot return a thing and then run the Redirect::to in a single request.

  • Bruni, I haven’t been able to yet. I would like to increase the example please ?

Browser other questions tagged

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