路由上的 Laravel 8 前缀在放置时不起作用,如果 url 不在代码上,则使用前缀

Laravel 8 prefix on route not working when placed and url works with prefix if its not on code

提问人:Austin Sanga 提问时间:11/15/2023 最后编辑:Austin Sanga 更新时间:11/15/2023 访问量:85

问:

我有一个路由文件夹 admin.php,具有以下预期代码

 Route::prefix('admin')->group(function(){
 Route::get('/new/dashboard',[SuperAdminController::class,'dashboard']);
 Route::get('/new/notifications', [SuperAdminController::class,'notifications']);
});

但是,如果我在浏览器上使用 http://127.0.0.1:8000/admin/new/dashboard 我会得到 404 |页面未找到。

但是,如果删除前缀,代码仍然存在

Route::get('/new/dashboard', [SuperAdminController::class,'dashboard']); 
   Route::get('/new/notifications', [SuperAdminController::class,'notifications']);

我去浏览器 http://127.0.0.1:8000/admin/new/dashboard 这条路线工作正常,即使我没有输入前缀 admin http://127.0.0.1:8000/new/dashboard 它仍然工作正常。

我尝试过检查 Routeserviceprovider 试图找到我可能定义它的任何其他方式,但无济于事。

我已经运行了所有的调试命令,

php 工匠路线:清除 , php 工匠配置:缓存, php 工匠路线:缓存 , php 工匠视图:缓存

并且相同的路由在服务器上工作正常,尝试拉取并确保所有代码都相同 stil 不起作用

这是管理员.php的摘要

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\SuperAdmin\SuperAdminController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\TransactionController;


Route::prefix('admin')->group(function(){
 Route::get('/new/dashboard', [SuperAdminController::class,'dashboard']);
    
    
    Route::get('/new/notifications', [SuperAdminController::class,'notifications']);




    Route::get('/new/schools/pending', [SuperAdminController::class,'schools_pending']);
    Route::get('/new/schools/pending/{id}', [SuperAdminController::class,'schools_pending_selected']);
    Route::get('/new/schools/query', [SuperAdminController::class,'schools_query']);
    Route::get('/new/schools/query/results', [SuperAdminController::class,'schools_query']);
    Route::get('/new/schools/query/results/{id}', [SuperAdminController::class,'schools_selected']);

    Route::get('/new/schools/suspended', [SuperAdminController::class,'schools_suspended']);
    Route::get('/new/schools/suspended/{id}', [SuperAdminController::class,'schools_suspended_selected']);

    Route::post('/new/schools/suspendschool', [SuperAdminController::class,'schools_suspending']);
    Route::post('/new/schools/unsuspendschool', [SuperAdminController::class,'schools_unsuspending']);
    Route::post('/new/schools/validate', [SuperAdminController::class,'schools_validate']);
 });
   

这是web.php的总结

<?php

use App\Http\Controllers\FavouriteController;
use App\Http\Controllers\SchoolApplicationController;
use App\Http\Controllers\TransactionController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\WebController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

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

/*
Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware(['auth'])->name('dashboard');
*/

require __DIR__ . '/admin.php';
require __DIR__ . '/auth.php';
require __DIR__ . '/school.php';

require __DIR__ . '/student.php';


Route::get('/changepassword/{id}/{pass}', [WebController::class, 'chpass']);



//Web Routes
Route::get('/', [WebController::class, 'landing']);

//Route::get('/dashboard', [WebController::class, 'checkoutrole']);

Route::get('/schools', [WebController::class, 'allschools']);

Route::get('/schools/level/{levelslug}', [WebController::class, 'school_by_levels']);

Route::get('/schools/{slug}', [WebController::class, 'schoolsIncategory']);

Route::get('/school/{slug}', [WebController::class, 'school_selected']);
Route::get('/school/{id}/books_rec', [WebController::class, 'book_recommended_byschool']);


Route::get('/book/{slug}', [WebController::class, 'book_selected']);
Route::get('/books', [WebController::class, 'allbooks']);

Route::get('/books/{slug}', [WebController::class, 'booksbycategory']);


php laravel-routing

评论

0赞 brombeer 11/15/2023
它显示正确吗?清除路由缓存是否有效?php artisan route:listphp artisan route:clear
0赞 Austin Sanga 11/15/2023
是的,我试过从 php artisan route:clear 到 php artisan config:cache, php artisan route:cache , php artisan view:cache
0赞 LF00 11/15/2023
当您不配置前缀时,您可以使用 route 进行访问。我想你的项目路径中有一个管理目录。admin/admin/new/dashboard
1赞 brombeer 11/15/2023
你说它在你的服务器上工作,所以很可能在你的本地端有问题。您忘记了某个地方的不同路由文件,可能是公共文件夹中的文件夹,对您没有上传的更改 - 不确定在哪里进一步查看。祝你好运(如果你能解决这个问题,请随时通知我们,谢谢)admin.htaccess
1赞 Austin Sanga 11/15/2023
@KamranAllana你能写出回复吗,真的很感激你把功劳记在心

答:

-1赞 lan3er 11/15/2023 #1

尝试是否应该将链接发送到该项目的 github,因为我需要 route.php 文件中的所有代码,也许您有路由层次结构问题,然后检查php artisan optimize:clearphp artisan route:list

0赞 Kamran Allana 11/15/2023 #2

1-定义前缀并添加文件RouteServiceProvider.php

    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::prefix('admin')
                ->middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/admin.php'));

            Route::prefix('api')
                ->middleware('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));
        });
    }

2-从中删除require __DIR__ . '/admin.php';web.php

3-展开路线Route::prefix('admin')->group(function(){});

4-确保公共文件夹没有确切的名称目录,例如admin