没有不值得去解决的问题,也没有不值得去学习的技术!

Simulate a Route for Resource Files in Laravel 6 With Database-Fetched Data

The corresponding controller method is implemented as follows. To comply with the Last-Modified specification, DATE_RFC7231 is used, which is the RFC 7231 format (example: Sat, 30 Apr 2016 17:52:13 GMT)

1. A route for requesting an asset file, for example: /static/xxx/9915995c-2952-490c-8e51-037a0950233c/assets/js/react.f886be.js

2. Because the asset_key contains /, and the Laravel routing component allows all characters except /, you must use a where condition with a regular expression to explicitly allow / to be part of the placeholder. Otherwise, the route will fall back. The routes/web.php file is used to define routes for the web interface.

PHP

Route::get('/static/xxx/{theme_id}/{asset_key}', 'ModelController@show')->where('asset_key', '.*');

3. Open https://regex101.com/ and confirm that the regular expression .* matches assets/js/react.f886be.js. See Figure 1.

Open https://regex101.com/ and confirm that the regular expression .* matches assets/js/react.f886be.js
Figure 1

4. The corresponding controller method is implemented as follows. To comply with the Last-Modified specification, DATE_RFC7231 is used, which is the RFC 7231 format (example: Sat, 30 Apr 2016 17:52:13 GMT). See Figure 2.

The corresponding controller method is implemented as follows. To comply with the Last-Modified specification, DATE_RFC7231 is used, which is the RFC 7231 format (example: Sat, 30 Apr 2016 17:52:13 GMT)
Figure 2
PHP

$model = Model::where('theme_id', '=', $themeId)->where('asset_key', '=', $assetKey)->where('category', '=', Model::CATEGORY_ASSET)->firstOrFail();
return response($model->content, 200)
	->withHeaders([
		'Content-Type' => $model->mime_type,
		'Last-Modified' => date(DATE_RFC7231, strtotime($model->getOriginal('updated_at'))),
	]);

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系