Home > Article > PHP Framework > One article introduces everything about Laravel Ignition
In this blog post, I will tell you everything about Ignition.
Let’s see what comes in PHP by default. PHP provides this functionality without using a framework. You just get errors: no stack trace, no request or application details.
Symfony's error page is slightly better, it shows you a stack trace, but it's not much help.
The screenshot below is of Whoops, which is standard in One article introduces everything about Laravel Ignition 5. It's much better than the default Symfony and can display stack traces and some information about the request. Although Whoops is the default in One article introduces everything about Laravel Ignition, it is framework-agnostic (error collection display). It only displays general information.
This is a screenshot of Ignition, the new error view we created. Since this is specific to One article introduces everything about Laravel Ignition, we can do a lot of cool stuff.
Let’s explore Ignition in all its details. It's open source and you can view the code here (https://github.com/facade/ignition).
If there are errors in the view, this is how whoops displays them. Note that the exception message does not fit in the allocated space. You have to hover (mouse) over it to see the full (information). In the stack trace, you can see that the compiled Blade views and content are used. This makes it difficult to track which Blade view file contains errors, and the view content itself is unreadable.
Ignition is a One article introduces everything about Laravel Ignition specific error page. Therefore, it can be "hooked" into the framework like a hook and used to expose uncompiled view paths and your Blade views. There's also enough space at the top to display the entire exception page without requiring additional clicks. We also only show application frames by default, as these may be the frames you are interested in.
If you click the pencil icon next to the file name to the right of the stack trace
tab, we will automatically open it in your favorite editor document. The default is PhpStorm. You can configure it as your favorite editor in the ignition
configuration file.
Notice that little "Telescope" link in the upper right corner? We'll only show it if you have One article introduces everything about Laravel Ignition Telescope (a first-party debugging assistant) installed. If you click on that link, you will be taken to an exception where something went wrong within the telescope.
If our default error screen is too bright, you'll be happy to know that our error pages also have a dark mode.
Let’s explore the tabs displayed on the Ignition
page.
Next to the Stack Trace tab, you will see the Requests tab. It shows all the expected information for your request.
Suppose you have a route definition like this::
Route::get('/posts/{post}', function (Post $post) { // });
When an exception occurs on this route, we will print the routing parameters in Ignitionpost
Model ( $post
variable), presented in the form of a converted array (toArray
). The same goes for "simple" route parameters that don't require any binding. This is a great way to easily see what information One article introduces everything about Laravel Ignition is receiving for this specific route.
After the route parameters, we will also show you the list of middleware used in this request.
Next is the "View" section. If the exception occurs in a view, we will display the view name here. Even more: we will also give a list of all the data passed to the view.
"用户" 选项卡包含有使用应用程序的用户和浏览器的更多信息。
在 Context
选项卡中,我们显示关于您的 repo
(repo位于何处,签出提交hash)和环境(您使用的 PHP 和 One article introduces everything about Laravel Ignition 的哪个版本)的信息。
在 Debug
选项卡中,我们将显示异常发生之前发生的事情。比如查询、日志和转储。在转储旁边,我们还显示您将 dump
语句放在何处的文件名。单击铅笔图标,您就可以直接访问该文件,并在您最喜欢的编辑器中纠正行号。
让我们来看一下另一个错误。这次我们将忘记导入 Class。Ignition 报错页面是这样的。
所以,Ignition 在看到异常是关于一个没有找到的 Class 时。它将尝试找出在其他命名空间中是否存在这个 Class。如果存在的话,它会建议我们导入。
Ignition 自带一系列常见问题的解决方案。若没有找到Blade视图,会采用一个One article introduces everything about One article introduces everything about Laravel Ignition Ignition如下所示。
您还可以自定义异常解决方案。需要异常类实现Facade\IgnitionContracts\ProvidesSolutions
接口。它要求您添加一个getSolution
方法。下面是一个可能的实现。
namespace App\Exceptions; use Exception; use Facade\IgnitionContracts\Solution; use Facade\IgnitionContracts\BaseSolution; use Facade\IgnitionContracts\ProvidesSolution; class CustomException extends Exception implements ProvidesSolution { public function getSolution(): Solution { return BaseSolution::create("You're doing it wrong") ->setSolutionDescription('You are obviously doing something wrong. Check your code and try again.') ->setDocumentationLinks([ 'Laracasts' => 'https://laracasts.com', 'Use Flare' => 'https://flareapp.io', ]); } }
下面是在 Ignition 中抛出异常的样子。
除了仅仅是建议的解决方案,我们也可以运行它们。想象一下,例如,您忘记设置app key
。这是用 Ignition 展示错误的样子。
如果你点击“生成app key”按钮,我们会在后台生成并设置app key
。
刷新页面后,应用程序将正常工作(除非它含有其他异常)
您可以通过让异常实现 Facade\IgnitionContracts\ProvidesSolution
来创建可运行的解决方案,这与不可运行的解决方案非常相似)。getSolution
方法既可以返回可运行的解决方案,也可以返回不可运行的解决方案。
namespace App\Exceptions; use Exception; use Facade\IgnitionContracts\ProvidesSolution; class CustomException extends Exception implements ProvidesSolution { public function getSolution(): Solution { return new MyRunnableSolution(); } }
下面是实现类MyRunnableSolution
.示例
namespace App\Solutions; use Facade\IgnitionContracts\RunnableSolution; class MyRunnableSolution implements RunnableSolution { public function getSolutionTitle(): string { return 'You are doing it wrong'; } public function getSolutionDescription(): string { return 'You are doing something wrong, but we can fix it for you.'; } public function getDocumentationLinks(): array { return []; } public function getSolutionActionDescription(): string { return 'To fix this issue, all you need to do is press the button below.'; } public function getRunButtonText(): string { return 'Fix this for me'; } public function run(array $parameters = []) { // Your solution implementation } public function getRunParameters(): array { return []; } }
以下是在 Ignition 中 如何抛出自定义异常 CustomException
的样子.
当用户点击Fix this for me
修复按钮时,run
函数将执行。
您可以将参数从异常发生的请求传递到将运行解决方案的请求。让getRunParameters
返回一个数组。该数组将被传递给 run
。
因此,你有能力使用文本或者可运行的解决方案来增强自己的异常。 但有时需要为内置的 PHP 异常,甚至是你无法控制代码的第三方异常提供友好的解决方案。
我们允许你使用 "Solution Providers" 来处理上面提到的难点。 Solution Providers 是可以通过 Ignition 挂钩到解决方案查找过程的类。 当异常被抛出并且 Ignition 接收到异常时,你可以调用自定义 solution provider 为这个异常返回一个或多个可能的解决方案。
例如,您可以创建一个自定义“堆栈溢出”解决方案提供程序,它将尝试为给定的异常找到匹配的堆栈溢出结果,并将它们作为解决方案返回。
我们也在 Ignition 自身上使用解决方案提供者。下面是这样一个解决方案提供者的样子:
use Throwable; use RuntimeException; use Facade\IgnitionContracts\Solution; use Facade\Ignition\Solutions\GenerateAppKeySolution; use Facade\IgnitionContracts\HasSolutionForThrowable; class MissingAppKeySolutionProvider implements HasSolutionForThrowable { public function canSolve(Throwable $throwable): bool { if (! $throwable instanceof RuntimeException) { return false; } return $throwable->getMessage() === 'No application encryption key has been specified.'; } public function getSolutions(Throwable $throwable): array { return [ new GenerateAppKeySolution() ]; } }
这些解决方案提供者可以在Ignition中自动注册,如下所示:
namespace App\Providers; use App\Solutions\GenerateAppKeySolution; use Facade\IgnitionContracts\SolutionProviderRepository; use Illuminate\Support\ServiceProvider; class YourServiceProvider extends ServiceProvider { /** * Register services. * * [@return](https://learnku.com/users/31554) void */ public function register(SolutionProviderRepository $solutionProviderRepository) { $solutionProviderRepository->registerSolutionProvider(GenerateAppKeySolution::class); } }
就像这样,方案提供者将继续增强 Ignitions 功能,为您的异常提供解决方案,我们迫不及待地想看看社区将提供什么!
Ignition 具有可扩展性。您可以添加新选项卡或替换默认选项卡。
Let’s take a look at the provided facade/ignition-tinker-tab. This package is a wrapper based on spatie/laravel-web-tinker that allows you to use Artisan tinker
in your browser.
With facade/ignition-tinker-tab installed, you can use Artisan tinker
on error pages.
We also created a second package called facade/ignition-code-editor. This tab replaces the default stack trace tab with a custom tab that allows you to edit code on the error screen. Here's what it does.
To learn how to add custom tabs, please visit the documentation on adding tabs.
Related recommendations:The latest five One article introduces everything about Laravel Ignition videos Tutorial
Original address: https://freek.dev/1441-ignition-a-new-error-page-for-laravel
Translation address :https://learnku.com/laravel/t/33857
The above is the detailed content of One article introduces everything about Laravel Ignition. For more information, please follow other related articles on the PHP Chinese website!