How to get PhpStorm to autocomplete facades in blade-files - laravel

When using #if (Auth::check()), PhpStorm doesn't recognize the Auth.
How do I tell PhpStorm that Auth is \Illuminate\Support\Facades\Auth?
Tested:
#php
use Illuminate\Support\Facades\Auth;
/** #var \Illuminate\Support\Facades\Auth Auth */
class Auth extends \Illuminate\Support\Facades\Auth {}
#endphp
#use(\Illuminate\Support\Facades\Auth)
neither worked, still get "Undefined Class Auth"
Edit 1:
the class Auth extends \Illuminate\Support\Facades\Auth {} line works if it's in another file, for example, the "_ide_helper.php", having it inside the blade file doesn't work.

IDE won't recognize methods accessed via the facade. laravel-ide-helper is a popular package that solves this problem. It generates a custom helper file that the IDE understands. This is not a complete solution but it covers most of laravel classes and helps with autocompletion. Here are your options.
Download and drop the latest _ide_helper.php file for laravel into your project from https://gist.github.com/barryvdh/5227822
Install the laravel-ide-helper package and let it generate a helper file on the fly. https://github.com/barryvdh/laravel-ide-helper
I'd personally suggest installing the package.

To get the right class use #if (\Auth::check())!

Related

Need install Laravel-ide-helper for VS Code

I use VS Code, not use PHP Storm. Should I install 'laravel-ide-helper'?
You can require this package with composer using the following command:
composer require --dev barryvdh/laravel-ide-helper
Then if you are using Laravel versions older than 5.5, add the service provider to the providers array in config/app.php:
Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class
In Laravel, instead of adding the service provider in the config/app.php file, you can add the following code to your app/Providers/AppServiceProvider.php file, within the register() method:
public function register(){
if ($this->app->environment() !== 'production') {
$this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
}
// ...
}
This basically allow your application to load the Laravel IDE Helper on non-production environments.
It does help with code completion (automatically importing classes, showing methods, etc..). It works with VSCode the same as it does for PHPStorm as far as I can tell. The 'ide-helper-models' also appears to work the same. I'm unsure what the 'ide-helper:meta' does, but the docs only mention support for PHPStorm.

Laravel PhpStorm autocomplete

I am having problems with autocompletion in PhpStorm using Laravel. I have set up the IDE accordingly from this guide:
https://confluence.jetbrains.com/display/PhpStorm/Laravel+Development+using+PhpStorm
I cannot autocomplete basic functions like Input::only or Input::has. The closest answer I can find is in this thread:
Laravel Intellisense / autocomplete with PhpStorm
However, Input is already added as alias in config/app.php - still not working.
Anyone experienced same problem and/or know a solution to this?
EDIT:
Sorry for not providing code example - it's only been some Laravel trial and error, but here goes:
I have the route:
Route::post('/login', 'LoginController#authenticate');
In the action of the controller I have tried the following:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
class LoginController extends Controller
{
public function authenticate(Request $request) {
Input::get(); // <-- autocompletes
Input::has(); // <-- NO autocomplete
$request->only(); // <-- autocompletes
$request->validate(); // <-- NO autocomplete
}
}
use laravel ide-helper package
it can provide accurate autocompletion. Generation is done based on the files in your project.
phpstrom support auto-complete best thing is that

Hot to use emcconville/google-map-polyline-encoding-tool in laravel

Hi i have installed emcconville/google-map-polyline-encoding-tool in Laravel, with composer, but cant see any references to the extension when i try yo use it in a class.
Do i need to register the extension anywhere in Laravel?
You don't need to register the package as an extension, the class Polyline coming from the package is automatically loaded with PHP/Composer autoload mechanism, so you can use it directly in your code (as per docs):
$points = array(
array(41.89084,-87.62386),
array(41.89086,-87.62279),
array(41.89028,-87.62277),
array(41.89028,-87.62385),
array(41.89084,-87.62386)
);
$encoded = \Polyline::encode($points);

PHPUnit Error: Call to undefined method Tests\Unit\ExampleTest::visit()

This is the test case I've written and when i try to run it with this command vendor/bin/phpunit on linux it gives me the error "Call to undefined method Tests\Unit\ExampleTest::visit()"
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* #return void
*/
public function testBasicTest()
{
$this->visit('/Login')
->type('KP123#gmail.com','email')
->type('123456','password')
->press('Login')
->seePageIs('/home')
->see('Katy Perry');
}
}
I've tried running composer update and it still could not work. Had Anyone experienced this issue before?
The visit() method looks like it's part of the SeleniumTestCase. So my guess is you either extend from the wrong base class. I assume it should look something like this:
class ExampleTest extends \PHPUnit_Extensions_Selenium2TestCase
{
// your test method
}
edit: I just noticed the Laravel tag, so it's probably more something like Illuminate\Foundation\Testing\TestCase. If this gives you errors that it can't find the class you have to set the bootstrap file in your phpunit.xml to the vendor/autoload.php or another appropriate bootstrap file where the file is registered in the autoloader.
I might have come in late onto this question but here is my 2 cents. I have been trying to use the same methods with my test case which led to a dismal failure.
I the realised that I could actually make use of the TestResponse class which is found on the Foundation namespace that wraps many methods around the response object.
$this->get('/url/to/visit'); //Then you can make your assertions on the returned response.
In my case I had authentication turned on for the application therefore it always complained about the header view where we display the username of the currently logged in user. If you run into that situation then use the Auth Facade like shown below:
\Auth::loginUsingId(1);
You should now be able to assert that your response has some text in it. Hope this helps. By the way I am on Laravel 5.4
If you are using new version of Laravel you must use Laravel Dusk to use similar methods to $this->visit or see. It was excluded from Laravel around version 6.
Instead you can use, but it has limited options:
$this->get('/Login')
composer require laravel/browser-kit-testing --dev
Try this
if you are using > Laravel 8 try :
$this->get('/Login')
instead of visit function, and assertSeeText instead of see

Laravel Controller not working

I'm very new to the Laravel framework and am trying to load a simple controller in my browser to slowly get the hang of things.
I have a file that's titled users.php inside of the the laravel/app/controllers/ folder and it looks like this:
class UsersController extends BaseController
{
public $restful = true;
public function action_index()
{
echo 'hi';
}
}
In the routes.php file, I have
Route::get('users', 'UsersController#index');
But, when I go to
http://localhost:8888/laravel/public/users
I'm greeted with a message that says "ReflectionException
Class UsersController does not exist"
I'm not sure if this is because I didn't install the mcrypt extension of PHP. But, when I checked the php.ini file on MAMP, it said that it was enabled. Upon entering
which PHP
in my terminal, it said /usr/bin/php. So, it might not be using the correct version of PHP.
I'm not entirely sure if this is a routes problem or if it's stemming from an absence of a vital PHP extension.
Thanks a bunch!
You need to use the Route::controller method to reference your Controller:
Route::controller('test', 'TestController');
...and rename your file (as Cryode mentions ) to be TestController.php.
Note - if you want to use the filename as test.php, then you will need to use composer to update the autoload settings.
Finally, the format of names for Controller methods changed in Laravel 4, try renaming the method
public function action_index() {}
to be
public function getIndex() {}
the get represents a HTTP GET request... the same applies for post (HTTP POST) and any (GET or POST.. )
I'm not familiar with that part of Laravel's source, so I'm not entirely certain that this is the issue, but your controller file name should match the controller class name, including capitalization.
So users.php should be UsersController.php. Now, when I do this myself on purpose, I get a "No such file or directory" error on an include() call, so that's why I'm not certain that's the sole cause of your problem. But it may be a start.

Resources