Code is not being read in full

Asked

Viewed 43 times

1

In my Controller file, only the 'Return View' is being read, the following code is passed directly and ends.

public function naocadastrado()
{
    return View('/naocadastrado');


    $user = new \App\User();
    $user->Name = Input::get('Name');
    $user->User = Input::get('User');
    $user->Password = hash('sha256', Input::get('Password'));
    $user->Tipo = Input::get('tipo_usuario');


        if("SELECT COUNT(*) AS total FROM tabela WHERE campo='User'"){

            return redirect('/cadastroexistente');

        }else{

            return redirect('/cadastrado');
        }

    $user->save();

}

public function naocadastrado()
    {
        return View('/naocadastrado');
    }

    public function ncadastrado()
    {
        $user = new \App\User();
        $user->Name = Input::get('Name');
        $user->User = Input::get('User');
        $user->Password = hash('sha256', Input::get('Password'));
        $user->Tipo = Input::get('tipo_usuario');


            if("SELECT COUNT(*) AS total FROM tabela WHERE campo='User'"){

                return redirect('/cadastroexistente');

            }else{

                return redirect('/cadastrado');
            }

        $user->save();

    }

I shared:

WEB.PHP

Route::group (['prefix' => '', 'namespace' => 'Api'], function(){

Route::get('/naocadastrado', 'CadastroController@naocadastrado');

Route::post('/naocadastrado', 'CadastroController@ncadastrado');
});

BLADE

<body>


    <form method="POST" action="/ncadastrado">

        <h1> Cadastro de Usuários UNIUS <br> </h1>
        <h2> USUÁRIO NÃO CADASTRADO<br> CADASTRE-SE! </h2>

        <label><b>Nome:</label> <input type="text" name="Name"> <br>
        <label><b>E-mail:</label> <input type="text" name="User"> <br> 
        <label>Senha:</label> <input type="password" name="Password" > <br>
        <label>Tipo de usuário</label>
        <select name="tipo_usuario">
                <option value="">Selecione</option>
                <option value="Administrador">Administrador</option>
                <option value="Desenvolvedor">Desenvolvedor</option>
        </select><br><br>              
        <input type="submit" value="Cadastrar" id="cadastrar" name="cadastrar">

    </form>





</body>
  • you are checking a string with something in if logo is always true.There is something missing.

  • 4

    will not continue anything even, right in the first line of the method already has the Return, after that you can put whatever you want it will never run.

  • Got it! Thank you xD

  • Why do you want to change the first line? As Marcelodiniz the remaining code will not be executed. And in the IF part, do the command to execute the query before and then compare to see if you returned any record, type rowCount or checking if it is true to run the block.

1 answer

2


You have a return in the first line of the function naocadastrado:

return View('/naocadastrado');

Any code following that line will not be executed. See what it says to documentation of PHP:

The Return statement returns control of the program for the module that called it. Execution will continue in the following expression to invoke the module.

If called within a function, the Return statement will immediately terminate its execution, and return its arguments as value to the function call.

That is to say:

function a() {
    echo "Executou a função 'a'";
    b();
}

function b() {
    echo "A função 'a' executou a função 'b'";

    return;

    c();
}

function c() {
    echo "A função 'b' executou a função 'c'";
}

a();

We’d have the following exit:

  • Executed the 'a function'
  • Function 'a' executed function 'b'

Note that in function b there is a return before calling the function c, so the function c will not be executed as the function b returned control to function a.

  • Still not going! You can see my code below?

Browser other questions tagged

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