How can I sent request to laravel component method?
In laravel 8 I create a new component with command :
php artisan make:component Frontend/Nomination
and I use it in my blade.php file as :
<x-frontend.nomination :defaultNomination="$defaultNomination" />
but as functionality has ajax requests I try to put some methods inside of this component and returning json
But adding line in routes/web.php:
Route::get('/get_nominated_photos/{nomination_id}/{current_page?}', View\Components\Frontend\Nomination::class)->name('get_nominated_photos');
I got error :
View\Components\Frontend\Nomination` was not found: Controller class `View\Components\Frontend\Nomination` for one of your routes was not found. Are you sure this controller exists and is imported correctly?
If there is a way to use Components in routes/web.php? If yes in which way ?
UPDATED BLOCK # : looks like I set the path invalid : So I modified line :
Route::get('/get_nominated_photos/{nomination_id}/{current_page?}', \App\View\Components\Frontend\Nomination::class)->name('get_nominated_photos');
and in component file : app/View/Components/Frontend/Nomination.php I have header :
<?php
namespace App\View\Components\Frontend;
class Nomination extends Component
{
...
But I got error :
Invalid route action: [App\View\Components\Frontend\Nomination].
at vendor/laravel/framework/src/Illuminate/Routing/RouteAction.php:92
88▕ */
89▕ protected static function makeInvokable($action)
90▕ {
91▕ if (! method_exists($action, '__invoke')) {
➜ 92▕ throw new UnexpectedValueException("Invalid route action: [{$action}].");
93▕ }
94▕
95▕ return $action.'@__invoke';
96▕ }
• `App\View\Components\Frontend\Nomination` was not found: Controller class `App\View\Components\Frontend\Nomination` for one of your routes was not found. Are you sure this controller exists and is imported correctly?
Thanks in advance!
As I said in the comment, you cannot render a Laravel Compoment that way unless your component doesn't have props and doesn't use slot.
The last release (v.8.80) of Laravel should help you achieve what you're trying to do with the pull #40425
Route::get(
'/get_nominated_photos/{nomination_id}/{current_page?}',
fn($nomination_id) => Blade::render('<x-frontend.nomination :defaultNomination="$defaultNomination" />', ['defaultNomination' => $nomination_id])
)->name('get_nominated_photos');