如何为我的水疗应用程序将laravel中的Route设置为例外?

时间:2019-04-21 13:32:12

标签: laravel routes

我已经为自己的作业开发了一个应用程序,它基于Vue.js, Laravel,基于教程,我的问题是基于我学到的教程,我已经编写了这个应用程序,现在我无法访问除了我的应用程序以外的任何路线。

我想创建一个关于页面,当我添加要路由的路由时,它将进入我的spa应用程序的默认页面,这是基于教程进行的,以防止人们在URL中输入废话,例如{{1 }},那么如何在该行中添加阻止我访问其他路由的例外?

代码如下:

url.com/asdasdqwe

因此,当我删除Route::get('/', function () { return view('welcome'); }); Auth::routes(); Route::get('/dashboard', 'HomeController@index')->name('dashboard'); // I know this line make my app to force it to don't access other routes like bottom('/about') // Route::get('{path}',"HomeController@index")->where( 'path', '([A-z\d-\/_.]+)?' ); // Because of top code i can't access other views that's using ('/etcroute') Route::get('/about', function () { return view('welcome'); }); 行时,我会在SPA应用程序中遇到一些问题。因此,我正在寻找一种添加异常或强制其使这些路由与{path}行兼容的方法。

3 个答案:

答案 0 :(得分:1)

欢迎使用StackOverflow:)

要解决此问题,只需将您的“ /关于”路线放置在与正则表达式匹配的那条路线上。

Laravel按照列出的顺序处理路线,因此您的“ / about”路线不会被看到,因为另一条路线首先与之匹配。

例如:

Auth::routes();

Route::get('/', function () {
   return view('welcome');
});

Route::get('/about', function () {
    return view('welcome');
});

Route::get('/dashboard', 'HomeController@index')->name('dashboard');

Route::get('{path}', 'HomeController@index')->where('path', '([A-z\d-\/_.]+)?');

答案 1 :(得分:0)

Route::get('dashboard/{any}', [HomeController::class, 'index'])->where(['any' => '.*']);

答案 2 :(得分:0)

像这样改变你的路由排序:

Route::get('/', function () {
   return view('welcome');
});

Auth::routes();

Route::get('/dashboard', 'HomeController@index')->name('dashboard');

// Because of top code i can't access other views that's using ('/etcroute')
Route::get('/about', function () {
    return view('welcome');
});

Route::get('{path}',"HomeController@index")->where( 'path', '([A-z\d-\/_.]+)?' );

Laravel 路由从 web.php 文件的顶部开始,并逐一检查路由。

因此您必须在静态路由的末尾添加正则表达式。

相关问题