我有一个简单的 Laravel Spark 应用程序,并试图从GitHub 上的 laravel.com 复制 /docs 代码。这些基本上是直接从该存储库复制的,版本数组已更改。
问题是访问 /docs/VERSION 返回 404。当访问根 /docs 时,它会点击 showRootPage() 函数并适当地转发到 /docs/0.1 - 但这是以 404 结尾的路由。所有文档/0.1/xyz 路由工作正常 - 例如文件 /resources/docs/0.1/documentation.md 存在并且 URL /docs/0.1/documentation 按预期工作。只有 docs/VERSION 路由有问题。
我在 show() 方法中看到了将其发送到默认值的代码,但它永远不会到达那里...您可以看到我在 DocsController 上的 show() 方法中有一个 dd()。返回 404 时它永远不会到达这一点,所以这似乎是一个路由问题。
任何关于该问题的想法将不胜感激!
这是我的路线/web.php
<?php
// srvAudit.com
Route::get('/', 'WelcomeController@show');
Route::get('/blog', 'WelcomeController@blog');
Route::get('/support', 'WelcomeController@support');
// The docs
define('DEFAULT_VERSION', '0.1');
function markdown($text)
{
return (new ParsedownExtra)->text($text);
}
Route::get('docs', 'DocsController@showRootPage');
Route::get('docs/{version}/{page?}', 'DocsController@show');
// The app
Route::get('/home', 'SessionController@index');
Route::get('/sessions', 'SessionController@listSessions');
Route::get('/sessions/{id}', 'SessionController@getSession');
Route::get('/sessions/server/{id}', 'SessionController@getServers');
Route::get('/sessions/user/{id}', 'SessionController@getUser');
以下是相关路线
root@312fedf0dc89:/var/www/test# artisan route:list|grep docs
| | GET|HEAD | docs | | App\Http\Controllers\DocsController@showRootPage | web,hasTeam |
| | GET|HEAD | docs/{version}/{page?} | | App\Http\Controllers\DocsController@show | web,hasTeam |
和 DocsController.php
<?php
namespace App\Http\Controllers;
use App\Documentation;
use Symfony\Component\DomCrawler\Crawler;
class DocsController extends Controller
{
/**
* The documentation repository.
*
* @var Documentation
*/
protected $docs;
/**
* Create a new controller instance.
*
* @param Documentation $docs
* @return void
*/
public function __construct(Documentation $docs)
{
$this->docs = $docs;
}
/**
* Show the root documentation page (/docs).
*
* @return Response
*/
public function showRootPage()
{
return redirect('docs/'.DEFAULT_VERSION);
}
/**
* Show a documentation page.
*
* @param string $version
* @param string|null $page
* @return Response
*/
public function show($version, $page = null)
{
dd($version);
if (! $this->isVersion($version)) {
return redirect('docs/'.DEFAULT_VERSION.'/'.$version, 301);
}
if (! defined('CURRENT_VERSION')) {
define('CURRENT_VERSION', $version);
}
$sectionPage = $page ?: 'srvAudit';
$content = $this->docs->get($version, $sectionPage);
if (is_null($content)) {
abort(404);
}
$title = (new Crawler($content))->filterXPath('//h1');
$section = '';
if ($this->docs->sectionExists($version, $page)) {
$section .= '/'.$page;
} elseif (! is_null($page)) {
return redirect('/docs/'.$version);
}
$canonical = null;
if ($this->docs->sectionExists(DEFAULT_VERSION, $sectionPage)) {
$canonical = 'docs/'.DEFAULT_VERSION.'/'.$sectionPage;
}
return view('docs', [
'title' => count($title) ? $title->text() : null,
'index' => $this->docs->getIndex($version),
'content' => $content,
'currentVersion' => $version,
'versions' => Documentation::getDocVersions(),
'currentSection' => $section,
'canonical' => $canonical,
]);
}
/**
* Determine if the given URL segment is a valid version.
*
* @param string $version
* @return bool
*/
protected function isVersion($version)
{
return in_array($version, array_keys(Documentation::getDocVersions()));
}
}
最后是我的 Documentation.php 模型
<?php
namespace App;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Contracts\Cache\Repository as Cache;
class Documentation
{
/**
* The filesystem implementation.
*
* @var Filesystem
*/
protected $files;
/**
* The cache implementation.
*
* @var Cache
*/
protected $cache;
/**
* Create a new documentation instance.
*
* @param Filesystem $files
* @param Cache $cache
* @return void
*/
public function __construct(Filesystem $files, Cache $cache)
{
$this->files = $files;
$this->cache = $cache;
}
/**
* Get the documentation index page.
*
* @param string $version
* @return string
*/
public function getIndex($version)
{
return $this->cache->remember('docs.'.$version.'.index', 5, function () use ($version) {
$path = base_path('resources/docs/'.$version.'/srvAudit.md');
if ($this->files->exists($path)) {
return $this->replaceLinks($version, markdown($this->files->get($path)));
}
return null;
});
}
/**
* Get the given documentation page.
*
* @param string $version
* @param string $page
* @return string
*/
public function get($version, $page)
{
return $this->cache->remember('docs.'.$version.'.'.$page, 5, function () use ($version, $page) {
$path = base_path('resources/docs/'.$version.'/'.$page.'.md');
if ($this->files->exists($path)) {
return $this->replaceLinks($version, markdown($this->files->get($path)));
}
return null;
});
}
/**
* Replace the version place-holder in links.
*
* @param string $version
* @param string $content
* @return string
*/
public static function replaceLinks($version, $content)
{
return str_replace('{{version}}', $version, $content);
}
/**
* Check if the given section exists.
*
* @param string $version
* @param string $page
* @return boolean
*/
public function sectionExists($version, $page)
{
return $this->files->exists(
base_path('resources/docs/'.$version.'/'.$page.'.md')
);
}
/**
* Get the publicly available versions of the documentation
*
* @return array
*/
public static function getDocVersions()
{
return [
'0.1' => '0.1',
];
}
}