The url does not have the category.
I am trying to reverse engineer how the menu view is passed data. The menu view is
@foreach ($categories as $category)
<?php $category = $category->present(); ?>
@if ($category->parent == null)
<li><a href="{{ $category->url }}">{{ $category->title }}</a></li>
@foreach ($category->children as $child)
<?php $child = $child->present() ?>
<li class="level1"><a href="{{ $child->url }}">{{ $child->title }}</a></li>
<?php
/*
@foreach ($child->children as $grandChild)
<?php $grandChild = $grandChild->present() ?>
<li class="level2"><a href="{{ $grandChild->url }}">{{ $grandChild->title }}</a></li>
@endforeach
*/
?>
@endforeach
@endif
@endforeach
So then I figured I should find where it is being passes $categories, and pass it the category of the current exercise or lesson.
There is no menu controller, but a menu composer:
<?php
namespace App\Http\Composers;
use Illuminate\Contracts\View\View;
use Cache;
class MenuComposer
{
public function compose(View $view)
{
$minutes = 6 * 60;
$value = Cache::remember('menu-categories', $minutes, function() {
return \App\Category::with('parent')->with('children')->get();
});
$view->with('categories', $value);
}
}
So it seems this where I should pass data to the menu view. But in this view, I am not sure how to access the data I need.
So I noticed an Items Composer, and I thought maybe that is where lesson and exercise data is available:
<?php
namespace App\Http\Composers;
use Illuminate\Contracts\View\View;
use Exception;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Helpers\QueryString;
use DB;
class ItemsComposer
{
private $viewData;
private $searchService;
public function __construct(\App\Services\SearchService $searchService)
{
$this->searchService = $searchService;
}
public function compose(View $view)
{
$this->viewData = $view->getData();
$view->with($this->createModel());
}
public function createModel()
{
$mode = $this->viewData['mode'] ?? 'lessons';
if ($mode == 'lessons') return $this->getLessons();
elseif ($mode == 'exercises') return $this->getExercises();
else throw new Exception('Unknown search mode.');
}
private function getLessons()
{
$result = $this->searchService->searchLessons($this->viewData);
return [
'lessons' => $result['lessons'],
'mode' => 'lessons',
];
}
private function getExercises()
{
$result = $this->searchService->searchExercises($this->viewData);
return [
'exercises' => $result['exercises'],
'mode' => 'exercises',
];
}
}
Anyway, I then decided to look at exercise view which is passes $exercise, and in controller I see this:
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
class ExerciseController extends Controller
{
public function getAll(Request $request)
{
$query = \App\Exercise::query();
if ($request->has('title')) {
$query->where('title', 'like', $request['title']);
}
if ($request->has('intro')) {
$query->where('intro', 'like', $request['intro']);
}
if ($request->has('free')) {
$query->where('free', $request['free']);
}
if ($request->has('id')) {
$query->where('id', $request['id']);
}
$query->orderBy('updated_at', 'desc');
return $query->get()->load('lesson')->load('tags');
}
public function get($id)
{
$exercise = \App\Exercise::find($id);
$exercise->load('lesson')->load('tags')->load('files')->load('coverPhoto', 'tabPhoto', 'fretPhoto');
$exercise->attachKindToFiles();
return $exercise;
}
public function post()
{
$exercise = \App\Exercise::create(Input::all());
isset(Input::all()['tags']) ? $exercise->updateTags(Input::all()['tags']) : '';
return $exercise;
}
public function put($id)
{
$exercise = \App\Exercise::find($id);
$exercise->update(Input::all());
$exercise->updateTags(Input::all()['tags']);
$exercise->updateFiles(Input::all()['files']);
$exercise->load('tags');
return $exercise;
}
public function delete($id)
{
\App\Exercise::find($id)->delete();
}
}
And in exercise view, accessing the category is as $exercise->lesson->category->title
When the item is a lesson, it is a similar call.
Above hasn't completely told me what I need to know but I feel like I am getting close. If an experienced larval developer can take a look at the above and describe what I need to do to get the item category passed to the menu view, I would be greatly appreciative!
thanks, Brian