I am trying to create an e-commerce site in Laravel.
getting this error :
Invalid route action: [HomeComponent].
I am getting errors like this :
at F:\xampp\htdocs\lv8ecom\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▕ }
i HomeComponent was not found: Controller class HomeComponent for one of your routes was not found. Are you
sure this controller exists and is imported correctly?
1 F:\xampp\htdocs\lv8ecom\vendor\laravel\framework\src\Illuminate\Routing\RouteAction.php:47
Illuminate\Routing\RouteAction::makeInvokable("HomeComponent")
2 F:\xampp\htdocs\lv8ecom\vendor\laravel\framework\src\Illuminate\Routing\Route.php:191
Illuminate\Routing\RouteAction::parse("/", ["HomeComponent", "HomeComponent"])
This error is saying that the route is not found, you can use your routes like this:
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index']);
Route::get = the request method type example (get / post / put / delete)
/home = the link you would like to access and call the controller at
App\Http\Controllers\HomeController::class = the controller name, you should create this file and name it as you like
'index' = the name of the method inside of the controller
Related
I use Laravel 5.8 and have test method like this this
public function test_store()
{
$attribute= ['lead_name'=>'test_name','lead_family'=>'test_family'];
$this->post('/leads', $attribute);
$this->assertDatabaseHas('leads', $attribute);
}
and controller like this:
class LeadController extends Controller
{
public function store(Request $request)
{
Lead::create($request->all());
}
}
and route :
Route::resource('/leads', 'App\Http\Controller\LeadController');
When I run phpunit show me this error:
Symfony\Component\HttpKernel\Exception\NotFoundHttpException: POST
http://localhost/myproject/public/leads
C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithExceptionHandling.php:118
C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:326
C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:120
C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php:375
C:\wamp64\www\myproject\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php:197
C:\wamp64\www\myproject\tests\Feature\Customer\LeadTest.php:38
I test leads URL with Postman and it work correctly.
I see and read this post https://stackoverflow.com/questions/23593892/symfony-component-httpkernel-exception-notfoundhttpexception-laravel
Set your router like this
Route::resource('leads', 'LeadController');
Make sure LeadController placed in app/Http/Controller
Read Laravel - Route::resource vs Route::controller
I solved it. I change to config\app.php file
'url' =>'http://localhost/myscript_addres/public',
To
'url' =>'http://localhost/',
I'm using Laravel 5.5.
My objective is to redirect to another method on the same controller to display a view with data.
class SeancesController extends Controller {
...
public function getRecommandations(Request $request) {
...
$data = [
'recommandationAutresActiviteMemeDateHeure' => $recommandationAutresActiviteMemeDateHeure,
'recommandationsMemeActiviteMemeHeure' => $recommandationsMemeActiviteMemeHeure,
'recommandationsMemeActiviteMemeDate' => $recommandationsMemeActiviteMemeDate
];
return redirect()->action('SeancesController#showRecommandations', $data);
}
public function showRecommandations(Request $request) {
return view('listeRecommandations', $request->data);
}
}
It is the right way to do this? Because I get this error :
InvalidArgumentException: Action App\Http\Controllers\SeancesController#showRecommandations not defined. in file /home/nicolas/public_html/M1_CSI/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php on line 338
I need to use action because I use an ajax call to access at getRecommandations().
I used this doc : http://laraveldaily.com/all-about-redirects-in-laravel-5/.
I didn't add a route that points to showRecommendations on my routing file. It's a problem?
Thank's for help!
I didn't add a route that points to showRecommendations on my routing file. It's a problem?
yes , it is a problem. because the redirector checks the routes that are assigned to the action and redirects the user to the route.
I'm trying to build RESTful API using CakePHP3. The problem is no matter what type of HTTP request I'm sending to, it always launches the index method.
My routes.php config:
/**
* REST Routes
*/
Router::scope('/articles', function (RouteBuilder $routes) {
$routes->resources('Articles');
});
Router::scope('/users', function (RouteBuilder $routes) {
$routes->resources('Users');
});
Router::scope('/categories', function (RouteBuilder $routes) {
$routes->resources('Categories');
});
The example controller looks like this:
public function initialize()
{
parent::initialize();
$this->autoRender = false;
$this->response->cors($this->request)
->allowOrigin(['*'])
->allowMethods(['GET', 'POST', 'PUT', 'DELETE'])
->build();
}
public function index()
{
echo 'index';
}
public function view($id = null)
{
echo "view:".$id;
}
public function add()
{
echo 'add';
}
I'm using Postman to send HTTP requests. The response I get from every type of request is:
Have a closer look at the docs again, that's not how you would connect ressource routes. They way you are doing it, ie passing additional scopes with the same name as the ressource, would create routes like
/users/users
(check bin/cake routes for a list of all connected routes), ie your visit to
/users
matches a different route, probably a fallback one.
You should connect your routes to the / scope instead (assuming that the /api/ part in your screenshot is part of the folder structure), like:
Router::scope('/', function (RouteBuilder $routes) {
$routes->resources('Articles');
$routes->resources('Users');
$routes->resources('Categories');
});
That will create routes in the form of
/users (GET)
/users (POST)
/users/:id (GET)
etc...
See also
Cookboook > Routing > Creating RESTful Routes
Cookboook > Shells, Tasks & Console Tools > Routes Shell
Within a Laravel package I made, I want to redirect the user to a controller action that requires a parameter (within the same package).
Controller:
public function postMatchItem(Request $request, $id)
{
$this->validate($request, [
'item_match' => 'required|numeric|exists:item,id',
]);
$spot_buy_item = SpotBuyItem::find($id);
$item = Item::find($request->input('item_match'));
$price = $item->getPrice();
$spot_buy_item_response = new SpotBuyItemResponse();
$spot_buy_item_response->spot_buy_item_id = $id;
$spot_buy_item_response->spot_buy_id = $spot_buy_item->spot_buy_id;
$spot_buy_item_response->item_id = $item->id;
$spot_buy_item_response->user_id = $spot_buy_item->user_id;
$spot_buy_item_response->spot_buy_price = $price;
$spot_buy_item_response->created_ts = Carbon::now();
$spot_buy_item_response->save();
return redirect()->action('Ariel\SpotBuy\Http\Controllers\Admin\SpotBuyController#getPart', [$id]);
}
The action in the redirect is the same path I use in my routes.php file to direct the user to this controller action
Route:
Route::get('/part/{id}', 'Ariel\SpotBuy\Http\Controllers\Admin\SpotBuyController#getPart')->where('id', '[0-9]+');
I've tried variations of this path without success, including SpotBuyController#getPart like the documentation suggests (https://laravel.com/docs/5.1/responses#redirects)
Note: I got this to work by naming my route in routes.php and using return redirect()->route('route_name', [$id]);, but I still want to know how to pass a package controller action to the ->action() function.
It's trying to access your controller from within the App\Http\Controllers namespace. Can see they've added it to your controller name in your error:
App\Http\Controllers\Ariel\SpotBuy\Http\Controllers\Admin\SpotBuyController#getPart
You need to escape the Ariel namespace with a \ at the start:
return redirect()->action('\Ariel\SpotBuy\Http\Controllers\Admin\SpotBuyController#getPart', [$id]);
I am currently trying to create a link on the index page that'll allow users to create an item. My routes.php looks like
Route::controller('items', 'ItemController');
and my ItemController looks like
class ItemController extends BaseController
{
// create variable
protected $item;
// create constructor
public function __construct(Item $item)
{
$this->item = $item;
}
public function getIndex()
{
// return all the items
$items = $this->item->all();
return View::make('items.index', compact('items'));
}
public function getCreate()
{
return View::make('items.create');
}
public function postStore()
{
$input = Input::all();
// checks the input with the validator rules from the Item model
$v = Validator::make($input, Item::$rules);
if ($v->passes())
{
$this->items->create($input);
return Redirect::route('items.index');
}
return Redirect::route('items.create');
}
}
I have tried changing the getIndex() to just index() but then I get a controller method not found. So, that is why I am using getIndex().
I think I have set up my create controllers correctly but when I go to the items/create url I get a
Unable to generate a URL for the named route "items.store" as such route does not exist.
error. I have tried using just store() and getStore() instead of postStore() but I keep getting the same error.
Anybody know what the problem might be? I don't understand why the URL isn't being generated.
You are using Route::controller() which does generate route names as far as I know.
i.e. you are referring to "items.store" - that is a route name.
You should either;
Define all routes specifically (probably best - see this blog here)
Use Route::resource('items', 'ItemController'); see docs here
If you use Route::resource - then you'll need to change your controller names
The error tells you, that the route name is not defined:
Unable to generate a URL for the named route "items.store" as such route does not exist.
Have a look in the Laravel 4 Docs in the Named Routes section. There are several examples that'll make you clear how to use these kind of routes.
Also have a look at the RESTful Controllers section.
Here's an example for your question:
Route::get('items', array(
'as' => 'items.store',
'uses' => 'ItemController#getIndex',
));
As The Shift Exchange said, Route::controller() doesn't generate names, but you can do it using a third parameter:
Route::controller( 'items',
'ItemController',
[
'getIndex' => 'items.index',
'getCreate' => 'items.create',
'postStore' => 'items.store',
...
]
);