Laravel action not defined error, but action is defined in controller - php

I keep getting an error that the action is not defined in my controller, but it is. I can access the index action, but not the processOrder action.
Below is my controller and my routes file.
namespace App\Http\Controllers\ThirdPartyAPI;
use App\Order;
use App\ThirdPartyAPI;
use GuzzleHttp\Client;
use App\Jobs\ThirdParyOrders;
use App\ThirdParty\ThirdPartyAPI;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class OrdersController extends Controller
{
public function index ()
{
// list orders
}
public function processOrder()
{
// some logic here
}
}
If my I call the action "#index" in my routes/web.php file, it works and it returns the url, but if I change the "#index" to "#processOrder", it throws the error.
Ie. this works:
Route::get('thirdparty/process-order', function() {
return action('ThirdPartyApi\OrdersController#index');
});
But this doesn't:
Route::get('thirdparty/process-order', function() {
return action('ThirdPartyApi\OrdersController#processOrder');
});
I'm not sure where I'm missing the plot.
I've tried to quit and then re-run:
php artisan serve
I've also tried
composer dump-autoload

Still not sure what the problem was initially, but I've managed to get it working by using a different method.
Intead of using a closure, I do it like this:
Route::get('thirdparty/{thirdparty_client}/process-order/{order}', 'ThirdPartyApi\OrdersController#processOrder');
This seems to do the trick. I didn't know that I could pass multiple parameters to the controller in this way, but this is working 100%.

I think you should have to try this as route.
Route::any("thirdparty/process-order", "ThirdPartyApi\OrdersController#processOrder");

Related

Laravel not recognizing Controller __invoke()

Can't debug this simple routing issue, despite going through several similar posts.
TestController is not invokable. The controller class TestController
is not invokable. Did you forget to add the __invoke method or is the
controller's method missing in your routes file?
I have played around with it as many permutations as I can find on Stack and nothing changes it. I have confirmed that simple routing, ie:
Route::get('/', function () {
return view('welcome');
});
works, but I can't get the controllers to work. I have cleared the cache and uncommented the $namespace, nothing makes a difference. In fact, the error message doesn't seem to change, which leads me to believe it's not the routing but something to do with the Controllers. But I am a newb and am not seeing it.
from web.php:
Route::get('/test', TestController::class);
TestController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class TestController extends Controller {
public function __invoke() {
return view('welcome');
}
}
This normally happens when you do not have the use statement for the FQCN of the controller.
In the routes file where you are declaring the route ensure that the use statement for the Controller namespace is added. Especially since Laravel 8.x the default namespace for controllers is not set to App\Http\Controllers unlike the earlier versions.
//routes file
use App\Http\Controllers\TestController;
Route::get('/test', TestController::class);
Just as a side note if you are using an invokable controller class to return just a view, you can use the Route::view() method instead
//Assuming that you have a resources/views/test.blade.php
Route::view('/test', 'test');
Maybe this helps someone similar to me, who overlooks a simple problem:
I wrote:
Route::post('controller/{resource}/action', \App\Http\Controllers\MyController::class, 'action')->name('controller.action');
Instead what I needed was:
Route::post('controller/{resource}/action', [\App\Http\Controllers\MyController::class, 'action'])->name('controller.action');
So basically I was missing the [] around the controller class and action params, they go together in an array - got to remember that. :)
Sorry if it is a just a tiny bit off-topic, but I searched for my problem and ended up here, therefore this just might help someone else too.
I don't know if you still need to hear this but change your
Route::get('/test', TestController::class);
into
Route::get('/test', [TestController::class, #MethodName]);
Also, call your model into the Controller.file
use App\Models\Test;

Laravel 5.6 Custom method to call a view

I have application in laravel.When I try to access a custom method using URL localhost/blog/public/contact/myownview it doesn't give any output. I want to redirect it to view called video.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ContactController extends Controller
{
//My Custom method
public function myownview(){
echo "yest";
//return view('video');
}
}
routes/web.php
Route::get('/contact/myownview','ContactController#myownview');
Try this
Route::get('/my-own-view', 'ContactController#myownview')->name('my-own-view);
and hit the http://localhost:8000/my-own-view, <url>+route name
return view('video');
Make sure in resources/views file has video.blade.php
You need to custom your route.php
Route::get('/blog/public/contact/myownview','ContactController#myownview');

Use controller namespace from route closure

I want a Route::group() to use a particular namespace using a closure rather than using the laravel syntax. So instead of
Route::group(['namespace' => 'My\Namespace\For\Controllers'), function () {
// TestController found in `My\Namespace\For\Controllers`
Route::resource('resource/url', TestController#test)
}
I wonder if it is possible to have something like
Route::group(function () {
// Some logic for using the `My\Namespace\For\Controllers`
// namespace for all routes within this group
// Controller found in `My\Namespace\For\Controllers`
Route::resource('resource/url', Controller#test)
}
I want this functionality so I can decide the controller name-space dynamically depending on a parameter passed to a route.
I guess the right way to do that is to create middleware, but definetely not route.php file.
https://laravel.com/docs/5.2/middleware
Update
If you need just to set namespace for all controllers in a group, you can do it like this:
Route::group(['namespace' => 'My\Namespace\For\Controllers'], function() {
// Controllers within the "My\Namespace\For\Controllers" namespace
});
https://laravel.com/docs/5.2/routing#route-group-namespaces

Laravel 5 / Lumen Request Header?

So I am not really sure how to go about this I have tried a few things and I will list one below however what I am trying to do is store information sent in a http request in a PHP variable.
Here is a view from Chrome Postman of me sending the request I want ot send. Note "pubapi" is a "header".
I have been messing around with Lumen requests as you can see documented here ( http://lumen.laravel.com/docs/requests ) and have tried using the following below to possibly display them but its not working obviously.
echo Request::all();
I am putting this in my controller and I have ...
use Illuminate\Http\Request;
in my controller.
So how could I say store the header I am sending "pubapi" into a php variable in my controller?
EDIT
Not sure if this will help, however looking at the Laravel frameworks docs I see this http://laravel.com/api/5.0/Illuminate/Http/Request.html#method_header trying this throws the same error in my code. So for example I tried the following and reached the same error.
echo Request::header('pubapi');
You misunderstand the Laravel request object on two levels.
First, the error you are getting is because you were referencing the object instead of the Facade. Facades have a way of forwarding static method calls to non-static methods.
Second, you are sending the value as a header but are trying to access the request parameters. This will never give you what you want.
Here is a simple way to see an example of what you want by creating a test route like so:
Route::match(['get','post'], '/test', function (Illuminate\Http\Request $request) {
dd($request->headers->all());
});
Post to this route and you will see your headers, one of which will be pubapi. Pay attention that the route method definition matches how you are submitting the request (ie GET or POST).
Let's apply this to the controller, ArticleController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ArticleController extends Controller
{
public function index(Request $request)
{
$pubapi = $request->header('pubapi'); // string
$headers = $request->headers->all(); // array
/*
$pubapi === $headers['pubapi']
*/
}
}
Try to change the Illuminate\Http\Request to Request.
- use Illuminate\Http\Request;
+ use Request;
Using
echo app('request')->header('pubapi');
Instead of
echo Request::header('pubapi');
Seemed to work perfect. Could someone provide additional explanation to why this worked and my original method didn't?
Actually you are calling it statically, that's why it is not getting appropriate Request class and throwing error, can do as follows
use Illuminate\Http\Request;
//inside your controller
class YourClass extends Controller{
public function yourFunction(Request $request){
//for getting all the request
dd($request->all());
//for getting header content
dd($request->header('pubapi'));
}
}
You can do it by request()->header('pubapi') also.
I prefer to use request() helper function than Request class its self. Because it can be used without use/import any class in the controller.

Laravel Controller in Folder - Routing doesn't work

I am putting my Controller called "LoginController" in a folder "login".
class LoginController extends BaseController{
public $restful = true;
//log in function
public function Login(){
// load the login page
return View::make('login.login');
}
}
In the routes, I give this:
Route::get('/',array('uses'=>'login.LoginController#Login'));
Also tried
Route::get('/',array('uses'=>'login\LoginController#Login'));
Route::get('/',array('uses'=>'login\Login#login'));
None of the above seem to work, and give me Class does not exist error.
I am very dumbstruck with this error. Is the way I am accessing the controller in the "uses" correct? Do I need to do any additional things before I can get it to work?
Any help really appreciated!
All you should need is
Route::get('/',array('uses'=>'LoginController#Login'));
Composer need to register this change in routes so dump-autoload composer
php composer.phar dump-autoload
Also if you are using laravel 4, then declaring restful controllers with
public $restful = true;
no longer works.
this happens to me often, just to give a different answer that worked for me
php artisan dump-autoload
Enjoy!
Yeah i had the same issue, i got my answer from https://stackoverflow.com/a/31638718/2821049
Route::group(['namespace' => 'login'], function(
{
// Controllers Within The "App\Http\Controllers\login" Namespace
Route::get('/','LoginController#login');
});
In class you adds :
namespace App\Http\Controllers\folder;
use App\User;
use App\Http\Controllers\Controller;
and in routes you call:
Route::get("admin/login","folder\class#NameFunctionInClass");
Note: folder is the name folder class contains

Categories