Welcome Guest, Not a member yet? Register   Sign In
Undefined variable: request;
#1

I recently migrated from CI 3 to 4 and I'm having a problem displaying $_POST values in my view. I have a filter in my view "listar.php" and when the form is sent the same view is loaded with new information based on the filter, but I cannot print the filter value when the page is loaded.
From what I noticed in CI 4 it is necessary to use
"echo $request->getGet('search');" to print the value sent by the form, but the problem happens because I'm not loading "Config\Services::request()", if I put " $request = \Config\Services::request(); " at the beginning of my view the error "Undefined variable: request" disappears, but it is very exhausting to always have to put this line in all views, I would like to know if anyone has a suggestion on how to always leave Request() loaded in all views by default, I would appreciate it.

Note: I haven't created the logic behind the filter yet, for now I just want to display what was sent in the filter itself
My controller: 


PHP Code:
namespace App\Controllers;
use 
App\Models\Modulo;

class 
Modulos extends My_Controller
{
    public function Getlistar()
    {
        $obj = new Modulo();
        $data['objetos'] = $obj->find();

        return view('estrutura/menu')
            view($this->controllerName.'/listar'$data)
            view('estrutura/rodape');
    }


Input I want to print the value: 
PHP Code:
<input type="text" name="busca" id="busca" value="<?php echo $request->getGet('busca'); ?>" class="form-control"
Reply
#2

What did you do in CI3?
and the code does not work in CI4?
Reply
#3

(04-11-2024, 05:35 PM)kenjis Wrote: What did you do in CI3?
and the code does not work in CI4?

Hello, in CI3 i would do the following:

PHP Code:
<input type="text" name="busca" id="busca" value="<?php echo $this->input->get('busca'); ?>"


that is, i would use "this->input->get('busca')" after submitting the page. 
I didn't participate in the creation of the project so i don't know where "$this->input->get()" came from, when i arrived at the company everyione used it like that.


Today i was reading about the use of CI4 "->withInput()" and it's basically what i need, so i could use "echo old('busca')" in the view, but i don't know how to put withInput when loading the view.

Use "$request = \config\services::request();" also solves my problem, but i don't know how to always load this service in all system views automatically, without having to declare it at the beginning of the file.
Reply
#4

Oh, that code is the CI3 era code when MVC was not properly separated.
In CI4 there is no `$this->input` in the View. So it does not work.

withInput() is used when you want to get variables after redirection.
So it is not for your case.
Reply
#5

This is a sample to pass Request to a view.
https://codeigniter4.github.io/CodeIgnit...parameters
If you write code in BaseController, you can pass it to all views.

PHP Code:
<?php

namespace App\Controllers;

class 
Home extends BaseController
{
    public function index(): string
    
{
        $view = \Config\Services::renderer();
        $view->setVar('request'$this->request);

        return view('welcome_message');
    }



PHP Code:
<?= esc($request->getGet('busca')) ?>

By the way, if you have the following code, it has XSS vulnerability. Don't use code like this.
You must escape all user inputs when you show them in HTML.
PHP Code:
<input type="text" name="busca" id="busca" value="<?php echo $this->input->get('busca'); ?>"
https://codeigniter4.github.io/CodeIgnit...s.html#esc
Reply
#6

(04-12-2024, 05:46 PM)kenjis Wrote: Este é um exemplo para passar Request para uma view.
https://codeigniter4.github.io/CodeIgnit...parameters
Se você escrever código no BaseController, poderá passá-lo para todas as visualizações. A propósito, se você tiver o código a seguir, ele possui vulnerabilidade XSS. Não use código como este. Você deve escapar de todas as entradas do usuário ao mostrá-las em HTML. https://codeigniter4.github.io/CodeIgnit...s.html#esc

PHP Code:
<?php

namespace App\Controllers;

class 
Home extends BaseController
{
    public function index(): string
    
{
        $view = \Config\Services::renderer();
        $view->setVar('request'$this->request);

        return view('welcome_message');
    }



PHP Code:
<?= esc($request->getGet('busca')) ?>



PHP Code:
<input type="text" name="busca" id="busca" value="<?php echo $this->input->get('busca'); ?>"

Thank you very much, that was really what i needed. I also thank you for the tip on using "esc()", it's something i wasn't aware of, thank you very much.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB