bmewburn / vscode-intelephense

PHP intellisense for Visual Studio Code
https://intelephense.com
Other
1.64k stars 94 forks source link

Undefined method 'getVar' with CodeIgniter #1670

Closed lipsaraiva closed 3 years ago

lipsaraiva commented 3 years ago

When used the methods 'getPost', 'getGet' or 'getVar' from CodeIgniter 4 framework in VSC, an alert appears to me: "Undefined method 'getVar'.intelephense(1013)".

To Reproduce $data = array( 'id' => $this->request->getVar('id'), 'nome' => $this->request->getVar('nome'), 'apelido' => $this->request->getVar('apelido') );

Expected behavior The code is OK, The alert should not appear.

Screenshots image

Platform and version Windows 10, CodeIgniter 4 and Intelephense 1.6.3.

TCGWebDev commented 3 years ago

I have the same problem...

bmewburn commented 3 years ago

If you look at the src for 4.0.1 $request is typed as HTTP\IncomingRequest which is the concrete class that declares a getVar function. Then in 4.1 it has become RequestInterface which does not declare getVar.

You may be able to override it by creating a non-executable ide helper file in your project root that types $request as the concrete type.

namespace CodeIgniter;

/**
 * Class Controller
 */
class Controller
{
    /**
     * Instance of the main Request object.
     *
     * @var HTTP\IncomingRequest
     */
    protected $request;
}
reizaoxaro commented 3 years ago

I got same problem

bmewburn commented 3 years ago

Closing this as it is working as designed. Workaround provided above. Looks like ci uses @mixin on the request interface which when implemented will resolve this #123

tsykiw commented 3 years ago

If you look at the src for 4.0.1 $request is typed as HTTP\IncomingRequest which is the concrete class that declares a getVar function. Then in 4.1 it has become RequestInterface which does not declare getVar.

You may be able to override it by creating a non-executable ide helper file in your project root that types $request as the concrete type.

namespace CodeIgniter;

/**
 * Class Controller
 */
class Controller
{
  /**
   * Instance of the main Request object.
   *
   * @var HTTP\IncomingRequest
   */
  protected $request;
}

image

Work fine, thanks! ❤

fahmiegerton commented 3 years ago

I'm outside of the controller and made my own library and tried with this recommendation from the docs.

image

When I tried $someClass->getVar(), it said the method is undefined.

But I still got the issue, how do I fix it? Thanks.

rizkirakasiwi commented 3 years ago

i found this from the documentation: https://codeigniter.com/user_guide/concepts/http.html#working-with-requests-and-responses

i impemented like this and work!

class SomeClass extends BaseController{

    protected $mRequest;
    public function __construct()
    {
        $this->mRequest = service("request");
    }

    public function someFunction(){
        $this->mRequest->getVar();
    }
} 
wandaazhar007 commented 3 years ago

after public function store(){ you can put this code

$request = \Config\Services::request();

and change $this->request->getVar() to be $request->getVar()

informatika3052 commented 3 years ago

thank you

noumecha commented 3 years ago

thank to you all

olegopro commented 3 years ago

Just extends from BaseController not through Controller

JaibeyPisco commented 3 years ago

codeigniter 4 just extends from BaseController not Controller and by default will have that attribute

<?php  
namespace App\Controllers;
  use App\Models\Libro;
  class Libros extends BaseController{
      public function guardar(){
         $name=  $this->request->getVar("name");
      }
  }
?>
mishbakhulumam commented 2 years ago

its same problem but differend method i got error like this...

`?php

namespace App\Controllers;

use CodeIgniter\Controller; use App\models\M_Siswa;

class Siswa extends controller { public function __construct() { $this->model = new M_Siswa; }

public function index()
{
    $data = [
        'judul' => 'Data Siswa',
        'siswa' => $this->model->getAllData()
    ];

    echo view('templates/v_header', $data);
    echo view('templates/v_sidebar');
    echo view('templates/v_topbar');
    echo view('siswa/index.php', $data);
    echo view('templates/v_footer');
}

public function tambah()
{
    $data = [
        'nisn' => $this->request->getPost('nisn'),
        'nama' => $this->request->getPost('nama')
    ];

    $success = $this->model->tambah($data);
    if ($success) {
        return redirect()->to(base_url('siswa'));
    }
}

}

?>`

getPost cannot read kindly how fixed this?

Rajatsingh007 commented 2 years ago

Capture

Only one Thing is to do -> Change the YourController extends controller to YourCountroller extends BaseController

mishbakhulumam commented 2 years ago

Iam sorry sir!! Can you give me a example?

Jum, 24 Des 2021 pukul 13.09 Rajatsingh007 @.***> menulis:

[image: Capture] https://user-images.githubusercontent.com/87520507/147323174-01b6f19b-a7b8-4f3a-9a29-0de07586edc0.PNG

Only one Thing is to do -> Change the YourController extends controller to YourCountroller extends BaseController

— Reply to this email directly, view it on GitHub https://github.com/bmewburn/vscode-intelephense/issues/1670#issuecomment-1000673608, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANQ26JQ3FD2FKMV7J2HIEB3USQFCFANCNFSM4XO74PBA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you commented.Message ID: @.***>

Hazzanawarafik commented 2 years ago

i found this from the documentation: https://codeigniter.com/user_guide/concepts/http.html#working-with-requests-and-responses

i impemented like this and work!

class SomeClass extends BaseController{

    protected $mRequest;
    public function __construct()
    {
        $this->mRequest = service("request");
    }

    public function someFunction(){
        $this->mRequest->getVar();
    }
} 

thank you very much bro... solved

mishbakhulumam commented 2 years ago

Just gived protected $mRequest; ???

Rab, 26 Jan 2022 pukul 08.15 Hazzanawarafik @.***> menulis:

i found this from the documentation: https://codeigniter.com/user_guide/concepts/http.html#working-with-requests-and-responses

i impemented like this and work!

class SomeClass extends BaseController{

protected $mRequest;
public function __construct()
{
    $this->mRequest = service("request");
}

public function someFunction(){
    $this->mRequest->getVar();
}

}

thank you very much bro... solved

— Reply to this email directly, view it on GitHub https://github.com/bmewburn/vscode-intelephense/issues/1670#issuecomment-1021762446, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANQ26JVSCHBZCHTG3VE5WVLUX5DL3ANCNFSM4XO74PBA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you commented.Message ID: @.***>

nyderetna commented 1 year ago

Followed @Rajatsingh007 's solution.. worked like magic!!

`<?php

namespace App\Controllers;

use CodeIgniter\Controller; use App\Models\UserModel; use App\Controllers\BaseController; //use BaseController

class Login extends BaseController //use BaseController instead of Controller`