暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

php系列二之phpstorm Xdebug和laravel常见问题整理

开发架构二三事 2019-11-21
630

常见问题

1. 执行 php artisan migration:make 报 Command "migrate:make" is not defined?

因为 php artisan migrate:make 是 Laravel 4 的语法,而 Laravel5 已经换成了 php artisan make:migration

执行 php artisan make:migration table_name 会为每个表在工程的 database 目录下的 migrations 目录下生成一个 php 文件。

如果要将这些文件添加到库中生成对应的表则需要执行 php artisan migrate

2. 更新依赖时出问题了如何解决?

先 composer clearcache 清理包、仓库缓存,再用 composer update,如果不起效,就删掉 vendor 目录重新安装。

3. Laravel 源码的结构

  • app:网站的业务逻辑代码,例如:控制器/模型/路由等

  • bootstrap:框架启动与自动加载设置相关的文件

  • config:网站的各种配置文件

  • database:数据库操作相关的文件

  • public:网站的对外文件夹,入口文件和静态资源(CSS,JS,图片等)

  • resources:前端视图文件和原始资源(CSS,JS,图片等)

  • storage:编译后的视图、基于会话、文件缓存和其它框架生成的文件

  • tests:自动化测试文件

  • vendor:Composer 依赖文件

  • app/Http/Controllers:存放控制器

  • app/Http/Middleware:存放中间件

  • resources/views:视图路径  blade 视图

4. laravel 配置文件

  • .env: 环境配置文件

  • .env.example:.env 文件的一个示例

  • .gitignore: git 的设置文件,制定哪些文件会被 git 忽略,不纳入文件管理

  • composer.json: 网站所需的 composer 扩展包

  • composer.lock: 扩展包列表,确保这个网站的副本使用相同版本的扩展包

  • gulpfile.js:GULP 配置文件( GULP 后边会学到)

  • package.json: 网站所需的 npm 包

  • readme.md: 网站代码说明文件

  • app/Http/routes.php:网站的大多数路由都定义在该文件中,该文件将会被 App\Providers\RouteServiceProvider 类加载。

5. phpstorm 中使用 laravel 的方法

  • 安装 Laravel Plugin 插件

  • 安装 Laravel IDE Helper 代码提示

5.1. 使用 composer 安装插件

  1. composer require barryvdh/laravel-ide-helper

在 config 目录里的 app.php 文件中的'providers'添加如下内容

  1. Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,

5.2. 在 app 目录里的 Providers 目录里的 AppServiceProvider.php 文件中的

  1. public function register()里输入如下内容来注册


  2. if ($this->app->environment() !== 'production') {

  3. $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);

  4. }

5.3. 生成代码跟踪支持

  1. php artisan ide-helper:generate

5.4. php artisan serve 启动服务

artisan 的 serve 命令还支持两个参数:

  • host 设置主机地址

  • port 设置 web server 监听的端口号 例如:php artisan serve --port=8888

5.5. 如果添加了 debugbar 拓展

  1. composer require barryvdh/laravel-debugbar

页面下方会出现:

如何查看 phpinfo

创建一个简单的文本文档并命名为 phpinfo.php

代码如下:

  1. <?php


  2. phpinfo();


  3. ?>

放入 nginx 或 apache 中之后,通过浏览器访问这个文件即可显示 PHP 信息 如:http://192.168.1.100/phpinfo.php

配置 phpstorm+xdebug+laravel 环境来 debug 源码

1. 安装

安装 phpstorm 和 laravel 的部分这里不再多说,之前都有提到。

安装 xdebug:

  1. E:\software\nginx-1.10.3>php -v

  1. PHP 7.1.32 (cli) (built: Aug 28 2019 09:08:22) ( NTS MSVC14 (Visual C++ 2015) x64 )

  2. Copyright (c) 1997-2018 The PHP Group

  3. Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies

  1. 下载 Xdebug

  • 地址:https://xdebug.org/download.php 下载对应 php 版本的即可。

  • 将下载下来的 dll 文件放置 php 安装目录的 ext 目录中并重命名为 php_xdebug.dll

  • 配置 php.ini,添加以下配置:

  1. [xdebug]

  2. zend_extension="E:\software\php-7.1.32-nts-Win32-VC14-x64\ext\php_xdebug.dll"

  3. xdebug.remote_enable=1

  4. xdebug.remote_port=9000

  5. xdebug.remote_host=localhost

  6. xdebug.profiler_enable=1

  7. xdebug.remote_mode = "req"

  8. xdebug.trace_output_dir="./xdebug"

  9. xdebug.profiler_output_dir="./xdebug"

  10. xdebug.remote_handler="dbgp"

  11. xdebug.idekey = "phpstorm" //必填

  • 扫行php -m查看安装情况:

  1. C:\Users\admin>php -m

  2. [PHP Modules]

  3. bcmath

  4. bz2

  5. calendar

  6. Core

  7. ctype

  8. curl

  9. date

  10. dom

  11. exif

  12. fileinfo

  13. filter

  14. ftp

  15. gd

  16. gettext

  17. gmp

  18. hash

  19. iconv

  20. imap

  21. intl

  22. json

  23. ldap

  24. libxml

  25. mbstring

  26. mcrypt

  27. mysqli

  28. mysqlnd

  29. odbc

  30. openssl

  31. pcre

  32. PDO

  33. pdo_mysql

  34. PDO_ODBC

  35. pdo_pgsql

  36. pdo_sqlite

  37. pgsql

  38. Phar

  39. readline

  40. Reflection

  41. session

  42. shmop

  43. SimpleXML

  44. soap

  45. sockets

  46. SPL

  47. sqlite3

  48. standard

  49. tidy

  50. tokenizer

  51. wddx

  52. xdebug

  53. xml

  54. xmlreader

  55. xmlrpc

  56. xmlwriter

  57. xsl

  58. zip

  59. zlib


  60. [Zend Modules]

  61. Xdebug

可以看到Xdebug已经安装成功。

  • 配置phpstorm,主要参考下面两张图片 


  • laravel在phpstorm中的配置主要参考:https://www.jetbrains.com/help/phpstorm/laravel.html,这里只是列出Debug Artisan commands的phpstorm的配置: 

图中arguments配置的是artisan的命令。

源码跟踪

  1. Auth::attempt($username, $request->isRemember))

对于Auth的attempt方法,表面上来看我们无从找起,但是进入Auth类会发现,Auth 是通过 Facade 动态绑定的,绑定到哪里呢,进一步寻找我们发现 在 vendor/laravel/framework/src/Illuminate/AuthServiceProvider 中:

  1. class AuthServiceProvider extends ServiceProvider

  2. {

  3. /**

  4. * Register the authenticator services.

  5. *

  6. * @return void

  7. */

  8. protected function registerAuthenticator()

  9. {

  10. $this->app->singleton('auth', function ($app) {

  11. $app['auth.loaded'] = true;

  12. return new AuthManager($app);

  13. });


  14. $this->app->singleton('auth.driver', function ($app) {

  15. return $app['auth']->guard();

  16. });

  17. }

  18. }

默认Auth是绑定了AuthManager:

  1. <?php

  2. namespace Illuminate\Auth;


  3. use Closure;

  4. use InvalidArgumentException;

  5. use Illuminate\Contracts\Auth\Factory as FactoryContract;

  6. class AuthManager implements FactoryContract

  7. {

  8. use CreatesUserProviders;


  9. protected $app;


  10. protected $guards = [];


  11. public function guard($name = null)

  12. {

  13. $name = $name ?: $this->getDefaultDriver();


  14. return isset($this->guards[$name])

  15. ? $this->guards[$name]

  16. : $this->guards[$name] = $this->resolve($name);

  17. }


  18. public function getDefaultDriver()

  19. {

  20. return $this->app['config']['auth.defaults.guard'];

  21. }


  22. public function __call($method, $parameters)

  23. {


  24. return $this->guard()->{$method}(...$parameters);

  25. }

  26. }

并没有找到 attempt 方法,不过有一个__call 的魔术方法,那肯定是他里面没错了,为了快速找到他究竟是何方神圣,直接用

  1. dd(get_class($this->guard()));

输出为:

  1. Illuminate\Auth\SessionGuard

再往下的方法调用都可以通过debug的方式确定实现类了。这里就不再赘述,详情见:https://learnku.com/articles/5963/toggle-laravel-login-default-bcrypt-encryption-validation

初始化laravel程序时通过修改库的方式添加了一个用户,校验不通过的问题

Auth.attempt调用了:

  1. \Illuminate\Auth\SessionGuard::attempt:

  2. public function attempt(array $credentials = [], $remember = false)

  3. {

  4. $this->fireAttemptEvent($credentials, $remember);

  5. $this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials);

  6. // If an implementation of UserInterface was returned, we'll ask the provider

  7. // to validate the user against the given credentials, and if they are in

  8. // fact valid we'll log the users into the application and return true.

  9. if ($this->hasValidCredentials($user, $credentials)) {

  10. $this->login($user, $remember);

  11. return true;

  12. }

  13. // If the authentication attempt fails we will fire an event so that the user

  14. // may be notified of any suspicious attempts to access their account from

  15. // an unrecognized user. A developer may listen to this event as needed.

  16. $this->fireFailedEvent($user, $credentials);

  17. return false;

  18. }

  19. /**

  20. * Determine if the user matches the credentials.

  21. *

  22. * @param mixed $user

  23. * @param array $credentials

  24. * @return bool

  25. */

  26. protected function hasValidCredentials($user, $credentials)

  27. {

  28. return ! is_null($user) && $this->provider->validateCredentials($user, $credentials);

  29. }

然后$this->provider->validateCredentials调用了:

  1. \Illuminate\Auth\EloquentUserProvider::validateCredentials:

  2. public function validateCredentials(UserContract $user, array $credentials)

  3. {

  4. $plain = $credentials['password'];

  5. return $this->hasher->check($plain, $user->getAuthPassword());

  6. }

这里会发现是通过hasher去check库里的密码和登录端传入的密码的,通过debug查看这个hasher为BcryptHasher的一个实例,于是对往库中添加的密码123456做如下处理:

  1. $hasher = new BcryptHasher();

  2. $hashPass = $hasher ->make("123456");

  3. printf($hashPass);


将打印出来的结果添加到库中的密码栏即可用该用户名与密码登录。

参考

  • laravel入门:https://segmentfault.com/a/1190000002505703

  • 环境配置:https://www.jetbrains.com/help/phpstorm/laravel.html

  • https://blog.csdn.net/u014665013/article/details/77758948

  • https://www.jianshu.com/p/15b496d8954c

  • https://segmentfault.com/a/1190000013025428

  • https://learnku.com/laravel

  • https://learnku.com/articles/5963/toggle-laravel-login-default-bcrypt-encryption-validation


文章转载自开发架构二三事,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论