gpt4 book ai didi

How to manage dynamic routing and static pages(如何管理动态路由和静态页面)

转载 作者:bug小助手 更新时间:2023-10-25 19:06:00 27 4
gpt4 key购买 nike



We build ecommerce and we need to have static rotes for pages like:

我们构建电子商务,我们需要为如下页面提供静态轮转:



  • About us

  • Home

  • Contact


For static pages links need to looks like this:

对于静态页面,链接需要如下所示:


www.example.com/home www.example.com/contact

Www.Example.com/home www.Example.com/Contact


On the other hand, we have product categories, subcategories and products whose servant is located in the database. Each category and product contains the full path in the slug table, for example:

另一方面,我们有产品类别、子类别和其仆人位于数据库中的产品。每个类别和产品都包含slug表中的完整路径,例如:


DB Slug: /car/bmw/parts/<product>

数据库插件:/car/bmw/Parts/


And link need to looks like this:
www.example.com/car/bmw/parts/<product>

链接需要如下所示:www.example.com/car/bmw/Parts/


It is clear to us that there is a conflict here, how to determine whether it is a static side or a dynamic side.
Paths must be exactly like this without adding /category or /product or /page/{page}. This is what our client requires

我们很清楚,这里有一个冲突,如何确定它是静态的一面还是动态的一面。路径必须与此完全相同,不能添加/CATEGORY或/PRODUCT或/PAGE/{PAGE}。这就是我们客户的要求


For categories, subcategories and products, we achieved a result, but we have a problem with static pages that the system treats as a category or product.

对于类别、子类别和产品,我们取得了结果,但系统将其视为类别或产品的静态页面存在问题。


Route:

路线:


// Route for category, sub-cat and products

Route::group(['namespace' => 'fronted', 'middleware' =>'web'], function () {
Route::get('/{categories?}', [FrontedController::class, 'index'])->where('categories', '^[a-zA-Z0-9-_\/]+$')->name('home');
});

// Static pages
// Homepage
Route::get('/', [HomePageController::class, '__invoke'])->name('home');

// About us
Route::get('/about-us', [AboutUsPageController::class, '__invoke'])->name('about-us');

// Shop / Products
Route::get('/products', [ShopPageController::class, '__invoke'])->name('shop');

// Blog
Route::get('/blog', [BlogPageController::class, '__invoke'])->name('blog');

// Contact
Route::get('/contact', [ContactPageController::class, '__invoke'])->name('contact');

Method

方法


public function index($categories= null)
{
$menu_items = Category::where('parent_id', null)->where('name', '!=', 'Root')->get();

// get categories in array
$cats = explode('/', $categories);

// make array to string used for slug
$cat_slug = implode('/', $cats);

// remove last url param. Used when you route only category without post
array_pop($cats);

// Its simple alias (name convention)
$post_slug = $cat_slug;

// No url params provided? Show home page

if(!$categories) {
return view('ecommerce::fronted.default.homepage',[
'menu_items' => $menu_items,
]);
}
// Category param exist

else if($categories) {

// Get category based on url slug
$category = Category::where('slug', $cat_slug)->with('children')->first();

// Here we must determine if category or post. If category not found we load post.

// Category does not exist! Load product
if(!$category) {
$product = Product::where('slug', $post_slug)->first();

return view('ecommerce::fronted.default.product',[
'menu_items' => $menu_items,
'product' => $product
]); }
// Category founded load it and their childs
else {
$products = $category->products;
return view('ecommerce::fronted.default.category',[
'menu_items' => $menu_items,
'products' => $products,
'category' => $category
]);
}

}
// Nothing founded! Show 404
else {
abort('404');
}

}

With this code we successfully route product categories, subcategories and products but we have problem when we call page like /about-us, /contact, /blog. He tries to find a category.

使用此代码,我们成功地传送了产品类别、子类别和产品,但在调用页面Like/About-Us、/Contact、/Blog时遇到了问题。他试图找到一个类别。


Is there a way to separate this?

有没有办法把它分开?


更多回答

Just move static routes to the top of api.php

只需将静态路由移动到api.php的顶部

优秀答案推荐

In Laravel the routes are loaded by its order in your routes file, meaning - the first matched route will be the one that laravel use.

在Laravel中,路由是按其在路由文件中的顺序加载的,这意味着第一个匹配的路由将是laravel使用的那条。


You can just add the static routes before the dynamic ones and it should solve it.

您只需在动态路由之前添加静态路由,它应该可以解决这个问题。


更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com