0

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!

Petro Gromovo
  • 1,755
  • 5
  • 33
  • 91
  • 1
    Check the View/Components folder. Is the component there somewhere? If so, what are its contents? – IGP Jan 16 '22 at 15:04
  • 1
    I believe it should be `\View\Components` not `View\Components` But that being said, I'm not sure Laravel Components can be rendered like this (Based on a similar question I saw some day ago on SO) – Clément Baconnier Jan 16 '22 at 15:25
  • Please look at UPDATED BLOCK – Petro Gromovo Jan 17 '22 at 05:01
  • The error explains it expect `_invoke` method since you're using [single action controller](https://laravel.com/docs/8.x/controllers#single-action-controllers) syntax. You should specify the method `render` – Clément Baconnier Jan 17 '22 at 07:49
  • I confirm what I was saying before, I tried and got a lot of issues. You can see some tries here to render a component out of blade: https://stackoverflow.com/questions/61484068/render-laravel-7-component-programmatically What you're trying will simply won't work unless your component doesn't have props and doesn't use slot. – Clément Baconnier Jan 17 '22 at 09:06

1 Answers1

1

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');

Clément Baconnier
  • 5,718
  • 5
  • 29
  • 55