Return javascript result with controller

2

Is it possible inside 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');
    }
...

But the echo of javascript does not work inside the controller, only if I give a return , and if I give a return the rest of the controller function does not run .

What is the best procedure in this case?

Attempt1:

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>';
    }
}
    
asked by anonymous 07.02.2014 / 15:07

1 answer

3

You can not run JavaScript from within the Laravel Controller.

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

Everything indicates that you have a popup window to log in. You want the popup window to close and the browser to be redirected to a specific address.

The backend side should restrict itself to just checking the credentials, and returning if there was success or error . p>

  • If successful, you should also enable authentication cookie and enter 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 also preferably indicate what the error was, returning a JSON something like this: { "sucesso": false, "erro": "Senha incorreta." }

With this controller return, your client-side JavaScript should close the window and redirect, or display the error message.

Note : this is just one approach among several. (And I'm assuming you have a popup window open from another page.)

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

class testeController {

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

}

This knowledge is useful, but I do not think it applies to you.

    
07.02.2014 / 15:24