Controllers handle the logic behind user requests in your Pollora application. They work the same way as standard Laravel controllers with additional WordPress integration.
Pollora includes a FrontendController that handles the WordPress template hierarchy fallback. For custom logic, create your own controllers and use them with WordPress routes.
use App\Http\Controllers\BlogController;
Route::wp('home', [BlogController::class, 'index']);
Route::wp('single', [BlogController::class, 'show']);
Route::wp('page', [BlogController::class, 'show']);WordPress objects are automatically injected via type hints thanks to the WordPressBindings middleware:
namespace App\Http\Controllers;
class BlogController extends Controller
{
public function show(\WP_Post $post, \WP_Query $query)
{
return view('post', [
'post' => $post,
'query' => $query,
]);
}
}You can also access WordPress data through helper functions and Sage Directives in your Blade views without passing them from the controller.
For simple routes, use single-action controllers:
namespace App\Http\Controllers;
class HomeController extends Controller
{
public function __invoke()
{
return view('home');
}
}Route::wp('front', HomeController::class);php artisan make:controller BlogControllerLaravel's service container injects dependencies into controller constructors and methods:
namespace App\Http\Controllers;
use App\Services\PostService;
class BlogController extends Controller
{
public function __construct(
private readonly PostService $postService
) {}
public function index()
{
$posts = $this->postService->getLatest();
return view('blog.index', compact('posts'));
}
}For general controller concepts (resource controllers, middleware, form requests), see the Laravel documentation.