How can I sent request to laravel component method? - laravel

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

Related

Target class does not exist in Laravel

this is the route from api.php :
Route::apiResource('friend-request', FriendRequestController::class)->except('delete');
the controller already exists and i'm calling it on a store using a post request :
FriendRequestController.php
class FriendRequestController extends ApiController
{
public function store(StoreFriendRequest $request, FriendRequestService $friendRequestService,UserService $userService)
{
//code
}
i get this error after calling this route
message: "Target class [App\Http\Controllers\App\Http\Controllers\FriendRequestController] does not exist."
i guess it's because of the form request what do you think ?
This is very probably because the $namespace property of the app/Providers/RouteServiceProvider.php file is uncommented.
It means that every controllers in your route file will have the default \App\Http\Controllers namespace prefixed.
So \App\Http\Controllers\FriendRequestController becomes \App\Http\Controllers\App\Http\Controllers\FriendRequestController and so on...
Since you are using the "new" route declaration notation (using the fully qualified namespace), you don't need a "default" namespace:
Route::apiResource('friend-request', FriendRequestController::class)->except('delete');
// FriendRequestController::class === \App\Http\Controllers\FriendRequestController
// However in the old declaration:
Route::apiResource('friend-request', 'FriendRequestController')->except('delete');
// You were only using the base class name, so the prefix was here to help Laravel find the "entry point" of your controllers
// which is \App\Http\Controllers\
All this is detailled in the migration guide: https://laravel.com/docs/8.x/upgrade#automatic-controller-namespace-prefixing

How to write route for breadcrumb in laravel?

Route::get('/usersearch', 'UsersController#usersearch');// for this url write breadcrumb
Route::Resource('user','UsersController');
how can i write breadcrumb route ? I want display on index.blade.php which is located in user folder
Laravel resource routing assigns the typical CRUD routes to a controller with a single line of code. See Official Documentation
In your case, route :
Route::resource('user','UsersController');
UsersController :
public function index()
{
return view('user.index'); // views/user/index.blade.php
}
Install Laravel Breadcrumbs
github.com/davejamesmiller/laravel-breadcrumbs
Run this at the command line:
composer require davejamesmiller/laravel-breadcrumbs:5.x

Laravel 5.7 ApiResource GET params empty

I use Laravel 5.7 for my JSON API web application.
In my routes/api.php file, I created the following route :
Route::apiResource('my_resource', 'API\Resource')->except(['delete']);
I added the corresponding controller and methods (index, show,...) and everythink work perfectly. My issue is the following : I would like to add optional GET params like this :
http://a.x.y.z/my_resource?param=hello&param2=...
And for instance being able to retrieve 'hello' in my index() method. However, when I print the value of $request->input('param'), it's empty. I just don't get anything.
Yet, if I create a route like this, with an optional parameter:
Route::get('/my_resource/{param?}', 'API\Resource');
I'm able to get the parameter value in my controller method.
Here is my index method :
class Resource extends Controller {
public function index(Request $request)
{
print($request->input('param'));
// ...
}
// ...
}
Am I missing something ? I'm still new in Laravel maybe I missed something in the documentation.
Thanking you in advance,
You can use:
$request->route("param");

Getting undefined call to function in Laravel 5.2

I am calling a simple function from a controller:
flash('my message');
flash function is inside a helpers.php file in App\Http
function flash($message)
{
$flash = app('App\Http\Flash');
return $flash->message($message);
}
flash function calls a Flash object
namespace App\Http;
class Flash{
public function message($message)
{
session()->flash('flash_message', $message);
}
}
Composer.json includes:
"autoload": {
"files": [
"app/Http/helpers.php"
],
Ran the command - composer dump-autoload
Page is showing - Call to undefined function App\Http\Controllers\flash()
I've tried so many things! Even if I add a tiny test function into the helpers.php file I can't use it in controller.
Is this a namespace issue? I didn't think I had to add a use xxxx; at top of controller as the helpers.php is added to autoload and global?
EDIT.
I believe that the registering of the helpers.php file is the key here. All I am doing is adding some functions to global file but cannot get to them from my controller. I added a really simple function to the helpers.php file yet couldn't access it from the controller: do I need to add anything to the controller in order to be able to use the helpers file?
I have managed to get this working by including the helpers.php file in the controller method:
include(app_path() . '/helpers.php');
flash('my message');
This now allows me to call the function. So the autoloading is not working! A little puzzled...
UPDATE.
I had to put the desired function into a class and call the class. This now works fine. I just couldn't get the helpers.php to autoload anything.
I would place the helper file in a global position and in your composer.json file:
"files":[
"app/helper.php"
],
In helper.php:
function newflash($message)
{
session()->flash('flash_message', $message);
}
Do a composer dump-autoload
It's as simple as this.
Now you can call the newflash() method from anywhere in your application.
Don't forget to register your class in the service provider.

Dynamic Routes not found

I am having trouble getting dynamic routes to work in Laravel 4
Routes
Route::any('/browse/{$id}', 'BrowseController#showProfile');
Controller
<?php
class BrowseController extends BaseController {
public function showProfile($id)
{
return $car_id;
}
}
When I go to http://localhost:8000/browse/10018
I receive a not found error
Any Idea what is wrong? Sorry I am new to Laravel
You don't need a $ in the variable name in your route. Try using
Route::any('/browse/{id}', 'BrowseController#showProfile');
Also, you should add validation to only allow numbers:
Route::any('/browse/{id}', 'BrowseController#showProfile')->where('id', '\d+');
The problem is in {$id}, try only {id}

Resources