Laravel : BadMethodCallException Method [show] does not exist - php

(1/1) BadMethodCallException
Method [show] does not exist. in Controller.php (line 82)
I am new to Laravel and PHP and have been stuck on this error for a very long time with other questions not providing a solution. I was following an example (where the example worked) and made very little changes beside name changes.
Here is the code:
web.php file
Route::get('/', 'PagesController#home');
Route::get('faq', 'PagesController#faq');
Route::resource('support', 'UserInfoController');
UserInfoController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\UserInfo;
class UserInfoController extends Controller
{
//
public function create(){
$userInfo = new UserInfo;
return view('contact', ['userInfo' => $userInfo]);
}
public function store(Request $request){
$this->validate($request, [
'name' => 'required',
'email' => 'required',
'subject' => 'required',
'description' => 'required',
]);
UserInfo::create($request->all());
return redirect()->route('contact')->with('success','Enquiry has been
submitted successfully');
}
}
UserInfo.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserInfo extends Model {
protected $fillable = [
'name','email','subject','description',
];
}
The Route::resource is the one giving me the problem as I am trying to access the page support/contact. Would be very grateful if someone knew how to solve this.

That is because you are doing resource routes in your routes.php file that generates all the routes for the CRUD functions when you have to generate a route for the show method you find that it does not exist.
To solve it only creates the methods that you ask or, also you can define only the routes that you need.

The controller is trying to invoke the 'show' method - which you should have defined if you're going to load /support/{id} via GET in your browser. You can see the expected methods for a resource here:
https://laravel.com/docs/5.4/controllers#resource-controllers
You can also make your life somewhat easier by starting with a valid controller by using the built in generator:
php artisan make:controller UserInfoController --resource
If you don't want to supply ALL the methods, you have to specify, for example:
Route::resource('support', 'UserInfoController', ['only' => [
'create', 'store'
]]);

Have you added method Show to your Controller ? Route::Resource has 7 basic routes:
Verb Path Action Route Name
GET /support index support.index
GET /support/create create support.create
POST /support store support.store
GET /support/{support} show support.show
GET /support/{support}/edit edit support.edit
PUT /support/{support} update support.update
DELETE /support/{support} destroy support.destroy
As you see there is a route called show which will be default when you route to support so you must connect this route to it's method in the controller which is in resource case CONTROLLER/show, however in your case you're trying to get a static page from a prefix called support which is different from resources because show in resource handling dynamic results.
Use this syntax to get a page called contact from prefix called support
Route::prefix('support')->group(function () {
Route::get('contact', function () {
// Matches The "/UserInfoController/contact" URL
});
});

Related

returning controller not found when using it as "controller#method" in laravel

I'm trying to make the resource route of steps work with id passed in the link like this "/steps/{howitwork:id}/create" and the create method looks like this:
public function create(HowItWork $how){
...
}
so before I define the resource route in the controller:
Route::get('steps/{howitwork:id}/create/', [
'as' => 'steps.create',
'uses' => 'StepController#create'
]);
what the app returning is:
Target class [StepController] does not exist.
you have add code under the resource route and try with this.
**Route::resrouce('something', SomethingController::class);
**Route::get('steps/{howitwork}/create', [StepController::class, 'create'])->as('steps.create');
I suggest you to use this syntax:
Route::get('/', [YourController::class, 'your-method'])->name('your-route-name')->middleware(['your-middleware-1', 'your-middleware-2', 'your-middleware-3']);

Laravel passport can't find ApproveAuthorizationController

I'm trying to setup a laravel application with oauth autorization by using the laravel passport functionality. I'm using the official tutorial (https://laravel.com/docs/master/passport). But now if I make a post request to '/oauth/authorize' the following error message occurs:
Class App\Http\Controllers\Laravel\Passport\Http\Controllers\ApproveAuthorizationController does not exist
I don't know what I've been doing wrong. I use the routes getting from 'Passport:routes' and no self defined routes.
I've already made a composer update, install and clear cache but nothing worked.
The problem get caused here:
/**
* Register the routes needed for authorization.
*
* #return void
*/
public function forAuthorization()
{
$this->router->group(['middleware' => ['web', 'auth']], function ($router) {
$router->get('/authorize', [
'uses' => 'AuthorizationController#authorize',
]);
$router->post('/authorize', [
'uses' => 'ApproveAuthorizationController#approve',
]);
$router->delete('/authorize', [
'uses' => 'DenyAuthorizationController#deny',
]);
});
}
I've already tried it by importing the missing class with a use statement but it still wont work.
Can somebody help me?
It looks like you're missing a use statement at the top of a controller or service proivder. Somewhere you have a class being used with out properly importing it first. That's why you're seeing the concatenated string like:
App\Http\Controllers\Laravel\Passport\Http\Controllers\ApproveAuthorizationController.
I assume what you need is this:
use Passport\Http\Controllers\ApproveAuthorizationController;
or Passport in Passport::routes is not being imported, one of the two. In AppServiceProvider:
use Laravel\Passport\Passport;

Method controller does not exist.

So I have used this format again. In my routes.php I have
Route::controller('datatables', 'HomeController', [
'PaymentsData' => 'payments.data',
'getIndex' => 'datatables',
]);
In my HomeController.php I have
public function getIndex()
{
return view('payments.index');
}
/**
* Process datatables ajax request.
*
* #return \Illuminate\Http\JsonResponse
*/
public function Payments()
{
return Datatables::of(DB::table('customer'))->make(true);
}
Anytime I try php artisan I get [BadMethodCallException] Method controller does not exist.
Question, is this form of doing it Deprecation or why anyone spot something wrong? Kindly assist. Thank you.
The controller method is deprecated since Laravel 5.3. But now, you can use the resource method, which is meant for the same purpose as the controller method:
From the docs:
Laravel resource routing assigns the typical "CRUD" routes to a controller with a single line of code. For example, you may wish to create a controller that handles all HTTP requests for "photos" stored by your application.
Use it as:
Route::resource('datatables', 'HomeController');
The downside of this implicit routing is that you have to name your methods consistently, more about it in the docs.
In most cases, better practise would be explicit routing, as it makes your code much more clear and understandable.
As far as I'm aware that's never been available for Laravel 5. I haven't used 4 so I'm not sure about prior to 5. But in 5 you need to use Route::get and Route::post.
Route::get('datatables', ['as' => 'HomeController', 'uses' => 'HomeController#getIndex']);
Route::get('payments-data', ['as' => 'HomeControllerPaymentsData', 'uses' => 'HomeController#Payments']);
Yep, it was removed as using implicit controllers is bad practice - https://github.com/illuminate/routing/commit/772fadce3cc51480f25b8f73065a4310ea27b66e#diff-b10a2c4107e225ce309e12087ff52788L259

Passing data from a route to a controller [Laravel]

I would like to pass a variable from my routes file to the controller specified. Not using parameters as the information is not in the URL. I can't figure out a way to pass it using the below code.
Route::get('faqs', [
'as' => 'thing',
'uses' => 'Controller#method',
]);
I know you can redirect to a controller as well but the error says that the method does not exist and after searching I found that the controller had to be assigned to a route and after that it was still the same error but in a different location.
Any thoughts?
One way this can be achieved is by creating custom middleware by adding your custom fields to the attributes property.
So you would do;
$request->attributes->add(['customVariable' => 'myValue']);
You can use Route::bind to assign value for specific slug
Route::bind('parameter', function($parameter)
{
return SomeModel::where('name',$parameter)->first();
});
Route::get('faqs/{parameter}', [
'as' => 'thing',
'uses' => 'Controller#method',
]);
And in your controller set this as a method parameter
public function method(SomeModel $model)
{
dd($model);
}

Unable to use Laravel / Socialite with Lumen

I try to use the package Laravel\Socialite in my system in Lumen (5.1)
I added this in the config\services.php file :
<?php
//Socialite
'facebook' => [
'client_id' => '##################',
'client_secret' => '##################',
'redirect' => 'http://local.dev/admin/facebook/callback',
],
In bootstrap\app.php file :
class_alias(Laravel\Socialite\Facades\Socialite::class, 'Socialite');
$app->register(Laravel\Socialite\SocialiteServiceProvider::class);
Then I created a controller for the facebook authentication :
<?php
namespace App\Http\Controllers\Facebook;
use App\Http\Controllers\Controller;
use Laravel\Socialite\Contracts\Factory as Socialite;
class FacebookController extends Controller
{
public function redirectToProviderAdmin()
{
return Socialite::driver('facebook')->scopes(['manage_pages', 'publish_actions'])->redirect();
}
public function handleProviderCallbackAdmin()
{
$user = Socialite::driver('facebook')->user();
}
}
And in the routes.php :
$app->get('/admin/facebook/login', 'App\Http\Controllers\Facebook\FacebookController#redirectToProviderAdmin');
$app->get('/admin/facebook/callback', 'App\Http\Controllers\Facebook\FacebookController#handleProviderCallbackAdmin');
I just followed the documentation, changing according to my needs. When I go to page http://local.dev/admin/facebook/login, I get the following error :
Non-static method Laravel\Socialite\Contracts\Factory::driver() cannot be called statically, assuming $this from incompatible context
Indeed, according to the code, driver function must be instanciate.
EDIT : And if I try to instanciate this class, I get the following error :
Cannot instantiate interface Laravel\Socialite\Contracts\Factory
How do you make this module to work?
here's how that works in my case
in services.php file
'facebook' => [
'client_id' => '***************',
'client_secret' => '***************',
'redirect' => ""
],
i left redirect empty cause my site is multilingual (so, it fills in a bit later with sessions). if you use only one language, put there your callback absolute path. for example
"http://example.com:8000/my-callback/";
also check your config/app.php. in providers array
Laravel\Socialite\SocialiteServiceProvider::class,
in aliases array
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
my routes look like this
Route::get('facebook', 'Auth\AuthController#redirectToProvider');
Route::get('callback', 'Auth\AuthController#handleProviderCallback');
here's auth controllers methods. put in top
use Socialite;
//იობანი როტ
public function redirectToProvider(Request $request)
{
return Socialite::with('facebook')->redirect();
}
public function handleProviderCallback(Request $request)
{
//here you hadle input user data
$user = Socialite::with('facebook')->user();
}
my facebook app
giga.com:8000 is my localhost (so its the same localhost:8000)
as you can see in Valid OAuth redirect URI, you should put there your callback. in my case i use three urls cause i have three languages. in your case it should be just
http://your-domain-name.com:8000/callback
if you work on localhost, you should name your domain in config/services.php
mine look like this
'domain' => "your-domain.com",
after everything run these commands
php artisan cache:clear
php artisan view:clear
composer dump-autoload
restart your server, clear your browser cookies. hope it helps

Categories