作为 PhpStorm 的忠实用户,使用 Laravel 的时候也是在使用 PhpStrom 来进行开发。然而我却发现了一些令我不解的问题。
Laravel 本身的依赖注入,服务提供者等特性使得 IDE 很难做到代码检查和智能提示,我理所当然地使用了 laravel-ide-helper,然而有些地方还是不能提示。
就拿一个 create_table 的文件来说。
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
}
这是 Laravel 自己生成的代码,然后我在 IDE 中发现 Schema 这个类还是没有实现智能提示,而且还有一个 method not found
的警告。
网上搜索之后发现,需要使用 \Schema
这个命名空间,而不是 Illuminate\Support\Facades\Schema
这个命名空间。这就很尴尬了,原本好好的命名空间机制,直接就回到了原始全是根命名空间的时代。
当然这也仅仅是一个例子,事实上还有很多类似的例子,需要使用根命名空间才能进行代码提示。
那么问题来了,laravel-ide-helper 这样的库,是否破坏了 Laravel 本身的特性(命名空间细化)。如果造成了破坏,如何能够优雅地解决这个矛盾,在不破坏 Laravel 本身特性的前提下,能够使用 IDE 进行代码提示,提高生产效率?
© 著作权归作者所有
发表评论