在Laravel 5.4路线上404

时间:2017-05-10 13:46:19

标签: php laravel laravel-spark

我有一个简单的Laravel Spark应用程序,我正在尝试从laravel.com复制/ docs代码,即on GitHub。这些基本上是直接从该存储库复制的,版本数组已更改。

问题是访问/ docs / VERSION返回404.访问root / docs时,它会命中showRootPage()函数并适当转发到/docs/0.1 - 但这是以404结尾的路由。所有docs / 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',
        ];
    }
}

0 个答案:

没有答案