The problem is after calling the parent index function the child index function is also invoked and the FeaturedPostsController
index function does not return the view.
If you call the parent::index($request);
from the child controller index function, still you won't be able to render the view because after executing the parent::index($request);
the control returns back to the caller function.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostsController extends Controller {
public function index(Request $request){
$viewData = $this->loadViewData();
$viewData['page_title'] = "Posts";
$viewData['posts'] = $this->posts($request);
return view('posts', $viewData);
dd('parent index function');
}
}
namespace App\Http\Controllers;
use App\Http\Controllers\PostsController;
use Illuminate\Http\Request;
class FeaturedPostsController extends PostsController {
public function __construct(Request $request) {
parent::index($request);
}
public function index(Request $request){
$viewData['page_title'] = "Featured posts";
dd('child index function');
}
}
Update your code with dd()
statements and then
dd()
in the parent controller to know the complete execution cycle.You can achieve your goal by using a protected $page_title
attribute and updating your controllers functions like :
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostsController extends Controller {
protected $page_title = 'Posts';
public function index(Request $request){
$viewData = $this->loadViewData();
$viewData['page_title'] = $this->page_title;
$viewData['posts'] = $this->posts($request);
return view('posts', $viewData);
}
}
namespace App\Http\Controllers;
use App\Http\Controllers\PostsController;
use Illuminate\Http\Request;
class FeaturedPostsController extends PostsController {
public function __construct(Request $request) {
$this->page_title = 'Featured posts';
parent::index($request);
}
}
Hope this helps you!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community