Creating route and controller to fetch the correct breadcrumb

parent deeae36a
<?php
namespace Modules\Media\Http\Controllers\Api;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Media\Entities\File;
use Modules\Media\Repositories\FolderRepository;
class FolderBreadcrumbController extends Controller
{
/**
* @var FolderRepository
*/
private $folder;
private $breadcrumb = [
//0 => 'Home',
];
public function __construct(FolderRepository $folder)
{
$this->folder = $folder;
}
public function __invoke(File $folder)
{
if ($folder->folder_id !== 0) {
$this->breadcrumb[] = ['id' => $folder->id, 'name' => $folder->filename];
}
$this->makeBreadcrumb($folder);
$this->breadcrumb[] = ['id' => 0, 'name' => 'Home'];
return response()->json(array_reverse($this->breadcrumb));
}
private function makeBreadcrumb($folder)
{
if ($folder->parent_folder === null) {
return;
}
$this->breadcrumb[] = ['id' => $folder->parent_folder->id, 'name' => $folder->parent_folder->filename];
if ($folder->parent_folder->folder_id !== 0) {
$this->makeBreadcrumb($folder->parent_folder);
}
}
}
......@@ -9,7 +9,10 @@ $router->group(['middleware' => 'api.token'], function (Router $router) {
'as' => 'api.media.folders.store',
'middleware' => 'token-can:media.folders.create',
]);
$router->get('folder/breadcrumb/{folder}', [
'uses' => 'FolderBreadcrumbController',
'as' => 'api.media.folders.breadcrumb',
]);
$router->post('folder/{folder}', [
'uses' => 'FolderController@update',
'as' => 'api.media.folders.update',
......@@ -50,6 +53,6 @@ $router->group(['middleware' => 'api.token'], function (Router $router) {
$router->get('media/{media}', [
'uses' => 'MediaController@find',
'as' => 'api.media.find',
'as' => 'api.media.media.find',
]);
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment