我有這樣的網址mysite.com/company,我想為網址添加前綴以成為mysite.com/home/company.
我試圖添加一個路由組,但這需要我更新所有現有的路由。
我可以在所有 url 中添加前綴而不影響現有路由嗎?
我使用了 Laravel 5.6
uj5u.com熱心網友回復:
我創建了一個沙箱,以便您可以查看和使用用于此答案的代碼。
我知道沙箱使用不同的 Laravel 版本(版本 7),但是查看5.6 版的檔案,路由似乎與版本 7 的路由沒有太大不同。
您可以做的是將已經存在的路由包裝在一個匿名函式中并將其分配給一個變數,然后您可以使用該變數并將其作為引數group與 a 一起傳遞給路由函式prefix,例如
$routes = function() {
Route::get('company', function () {
return 'companies';
});
Route::get('company/{company}', function ($company) {
return "company $company";
});
Route::delete('company/{company}', function ($company) {
return "deleting company $company...";
});
Route::get('company/{company}/staff', function ($company) {
return "staff list for company $company...";
});
};
Route::prefix('/')->group($routes);
Route::prefix('/home')->group($routes);
運行時php artisan route:list回傳以下內容:
-------- ---------- ------------------------------ ------ --------- ------------
| Domain | Method | URI | Name | Action | Middleware |
-------- ---------- ------------------------------ ------ --------- ------------
| | GET|HEAD | api/user | | Closure | api |
| | | | | | auth:api |
| | GET|HEAD | company | | Closure | web |
| | GET|HEAD | company/{company} | | Closure | web |
| | DELETE | company/{company} | | Closure | web |
| | GET|HEAD | company/{company}/staff | | Closure | web |
| | GET|HEAD | home/company | | Closure | web |
| | GET|HEAD | home/company/{company} | | Closure | web |
| | DELETE | home/company/{company} | | Closure | web |
| | GET|HEAD | home/company/{company}/staff | | Closure | web |
-------- ---------- ------------------------------ ------ --------- ------------
您可以在上面看到,現在可以通過/和訪問路線home/,例如http://example.com/company,http://example.com/home/company無需復制路線。
如果以后需要添加更多前綴,只需Route::prefix("<prefix>")->group($routes);向路由檔案添加一個新前綴即可。
下面的更新是對 OP 提供的評論的回應。
為了獲得正確的答案,您正在尋找一種方法來自動將url函式的所有實體從)轉換url('someurl')為url('home/someurl'),例如
url('company')會變成url('home/company')url('knowledgebase') 會變成url('home/knowledgebase')
如果是這樣,那么我為您提供兩種解決方案:
您不能在您使用的 IDE 中簡單地進行搜索和替換嗎?
您可以覆寫 Laravel 的
url輔助函式,為所有路徑添加前綴home/,為此您可以執行以下操作:
我創建了另一個沙箱,以便您可以查看和使用用于此答案的代碼。
首先helpers.php在 Laravel 應用程式中的任何位置創建一個檔案,在測驗此代碼時,我將該檔案放在應用程式的根目錄中,例如/var/www/html/helpers.php.
Second you need to override the url helper function, to make sure you don't lose any functionality I grabbed the original source code of the function from Laravel's github repository. I then modified it to include the prefix, so place the following in the new helpers.php file:
<?php
use Illuminate\Contracts\Routing\UrlGenerator;
if (! function_exists('url')) {
/**
* Generate a url for the application.
*
* @param string|null $path
* @param mixed $parameters
* @param bool|null $secure
* @return \Illuminate\Contracts\Routing\UrlGenerator|string
*/
function url($path = null, $parameters = [], $secure = null)
{
if (is_null($path)) {
return app(UrlGenerator::class);
}
$path = "home/$path";
return app(UrlGenerator::class)->to($path, $parameters, $secure);
}
}
Next, you need to load your helpers.php file before Laravel loads their helper functions, otherwise it wont load, to do so add require __DIR__.'/../helpers.php'; to public/index.php before require __DIR__.'/../vendor/autoload.php';, e.g.
require __DIR__.'/../helpers.php';
require __DIR__.'/../vendor/autoload.php';
Now you can use the new url function within your application, I have given some examples in the web.php routes file:
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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 () {
echo url("company");
echo "<br/>";
echo url("knowledgebase");
});
The above will output to the webpage:
http://example.com/home/company
http://example.com/home/knowledgebase
Now if you need to change the url function slightly to more fit your requirements you can do so by modifying it within the helpers.php file.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/326203.html
下一篇:我需要獲得最多點贊的5個帖子
