PHP code example of malyusha / path-history
1. Go to this page and download the library: Download malyusha/path-history library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
malyusha / path-history example snippets
Route::get('{category_slug}')->name('products_category')->uses('ProductCategoryController@showCategory');
Route::get('{category_slug}/{product_identifier}')->name('product')->uses('ProductsController@showProduct');
Route::get('{category_slug}/p-{product_identifier}')->name('product')->uses('ProductsController@showProduct');
namespace App\Entities;
use App\DescendantRetrievers\Shop\NestedSetDescendants;
use App\DescendantRetrievers\Shop\ProductsOfCategory;
class ProductCategory extends \Illuminate\Database\Eloquent\Model
{
use \Malyusha\PathHistory\HasPathHistory;
protected $updatePathOnChangeAttributes = ['parent_id'];
protected $parentPathRelation = 'parent';
protected $descendantRetrievers = [
NestedSetDescendants::class,
ProductsOfCategory::class,
];
public function parent()
{
return $this->belongsTo(static::class, 'parent_id');
}
public function shouldUseParentPaths(): bool
{
return ! $this->isRoot();
}
public function isRoot(): bool
{
return $this->parent_id === null;
}
public function products(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(Product::class);
}
}
// web.php
Route::get('/')->name('index')->uses('HomeController@showIndex');
// ... Other routes definitions ...
// And here comes shop section
Route::prefix('shop')->name('shop.')->group(function () {
Route::get('/')->name('index')->uses('ShopController@index');
// Call dynamic router registration
app('ph.router')->register();
});
public function showIndex()
{
$products = App\Entities\Product::with('currentPath')->get();
return view('index_page', ['products' => $products]);
}
return [
// other params here...
'paths' => [
[
'prefix' => 'news',
'types' => [
\App\Entities\Content\NewsCategory::class => \App\Http\Controllers\News\CategoryController::class,
\App\Entities\Content\News::class => \App\Http\Controllers\News\NewsController::class,
]
],
[
'prefix' => 'products',
'types' => [
\App\Entities\Shop\Product::class => \Admin\Http\Controllers\Shop\ProductsController::class,
\App\Entities\Shop\ProductCategory::class => \Admin\Http\Controllers\Shop\CategoryController::class,
],
],
],
];