Laravel - Обмен переменной между функциями контроллера - PullRequest
0 голосов
/ 01 декабря 2018

Я новичок в Laravel и работаю над простым проектом, где в моем контроллере (TestController) у меня есть две функции (обработка и показ) .

Я пытаюсь передать переменную $ email между функциями, чтобы я мог использовать ее в функции show, но я не знаю, как это сделать.

Контроллер

class TestController extends Controller
{    

    public function process(Request $request){
        if($request->ajax()) {
            $email = $request->get( "fullemail" );
        }
    }


    public function show(){

    }

}

Любая помощь будет оценена.Заранее спасибо :)


РЕДАКТИРОВАТЬ

Я отредактировал свой код следующим образом.В настоящее время я получаю Too few arguments to function App\Http\Controllers\TestController::show(), 0 passed and exactly 1 expected Ошибка.

Контроллер

class TestController extends Controller
{    
    public function process(Request $request){
        if($request->ajax()) {
            $email = $request->get( "fullemail" );
            $this->show($email);
        }
    }

    public function show($email){
        dd($email)
    }
}

1 Ответ

0 голосов
/ 01 декабря 2018

Если вы достигли уровня Laravel и знаете, что это за функции, вы, вероятно, уже знаете, как это сделать:

class TestController extends Controller
{    

    public function process(Request $request){
        if($request->ajax()) {
            $email = $request->get( "fullemail" );
            $this->show($email);
        }
    }


    public function show($email){
        // Do whatever you will with the $email variable.
        return view('some.view', ['email' => $email]);
    }

}

Альтернативный подход:

class TestController extends Controller
{    

    // Declare this variable as a class property - protected means, that only this class and any class that inherits from this class can access the variable.
    protected $email;

    public function process(Request $request){
        if($request->ajax()) {
            $this->email = $request->get( "fullemail" );
            $this->show();
        }
    }


    public function show() {
        // Do whatever you will with the $email variable.
        return view('some.view', ['email' => $this->email]);
        // On top of that, if you change $email value here, the changes will be available in all other methods as well.
        // $this->email = strtolower($this->email);
    }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...