Method App\\Http\\Controllers\\Controller::show does not exist - laravel-5

I just have upgraded one of my project from laravel version 5.2 to 7.1 But now I have started getting following error
Method App\\Http\\Controllers\\Controller::show does not exist.
I know there is not a function named show in my controller class. But if there is any way to trigger custom named method instead of show() while declaring resource attribute in web.php file.
Here is my resource declaration in web.php
Route::resources([
'first', 'FirstController',
'second','SecondController'
]);
Can I make some customization to tackle this situation.

Related

route model binding doesn't work anymore with api package devolpment in in Laravel 8.x

I created an api which is delivered with package development composer. in Laravel 7 it was possible to add the route model binding with:
Route::group(['namespace' => 'Acme\Package\app\Http\Controllers\Api'], function () {
// fomer possibility:
Route::apiResource('api/comments/', 'CommentController')->middleware('bindings');});
In Laravel 8 that's not possible anymore. I've tried almost everything the last days, but either the route-model-binding is not working, or the class can't be found:
Target class [bindings] does not exist.
I really hope someone can post an answer to the problem, or a hint or anything useful.
many thanks in advance
EDIT:
Thanks for the answers, including the middleware in the Route::group like mentioned:
Route::group(['namespace' => 'Acme\Package\app\Http\Controllers\Api', 'middleware' => 'Illuminate\Routing\Middleware\SubstituteBindings']
did it.
In Laravel 8 the alias for this middleware has been removed. You can either use it by its full class name
Illuminate\Routing\Middleware\SubstituteBindings
or add the alias back in to your app/Http/Kernels.php in $routeMiddleware as follows:
protected $routeMiddleware = [
'auth' => Authenticate::class,
'bindings' => Illuminate\Routing\Middleware\SubstituteBindings,
/*...*/
You have to be careful if you are relying on values in someones application to exist. I could use a different name/alias for that middleware if I wanted to in my HTTP Kernel. You should be using the FQCN for that middleware that comes from the framework when referencing it like that.
In a default Laravel 8 install there is no middleware named/aliased as 'bindings'. It is referenced by its FQCN, Illuminate\Routing\Middleware\SubstituteBindings, which is how you should probably be referencing it from a package.
You can provide a config file for someone to alter these things if you would like. Then you can use your configuration to know which middleware to reference.

Laravel 5.3 trying to get property of non-object

I just updated my application into version 5.3 and in many routes I'm getting
trying to get property of non-object
Like for example I have a resource called post and another called Article where each post can have one Articles. And each Article has one writer. When I do
$article->post->writer;
I get trying to get property 'writer' of non-object
Before updating from 5.2 this route as well as other routes worked perfectly. Am i missing something here? should i do something after composer update has been run successfully?
Edit: The article, the post and the writer all exist in the DB. When I try to return $article I get this.
{
"article": []
}
When i switch back to laravel 5.2 I get the proper article
Edit 2: it is routing bidnings problem. I'm using implicit model binding, so when i pass the ID of the article to my route, Laravel fetches the resource. but Now apparently it doesn't do it. I followed the instructions here for the bindings https://laravel.com/docs/5.3/upgrade#upgrade-5.3.0 but Laravel can't still get the article
It was name bindings issue. Laravel 5.3 needs some configuration in kernel.php as well as in the group route https://laravel.com/docs/5.3/upgrade#upgrade-5.3.0
The group should have 'middleware' => ['bindings']

Datattables Laravel 5.4 - FatalThrowableError Call to undefined function App\Http\Controllers\Datatables()

I'm using datatables with laravel 5.4, but when I use the datatables()->query():
in providers: Yajra\Datatables\DatatablesServiceProvider::class,
in aliases: 'Datatables' => Yajra\Datatables\Facades\Datatables::class,
My code:
return Datatables()->query( /**query**/ )
return this error in my console:
FatalThrowableError
App\Http\Controllers\Datatables()
I've tried datatables() and Datatables()
Someone with the same problem?
This error happens because Laravel cannot find your package Datatables.
Laravel in controllers, if you are not mapping your classes correctly will always seek the called classes at this point App\Http\Controllers.
Just put use DataTables; or use \Yajra\Datatables\Datatables; at the start of your controller and it shall be fixed. documentation
When you install any package through composer, composer uses namespace to find the package and route functions and classes to it.
you can read more about the namespaces from php.
and you can check this tutorial for namespaces.
Also, Laravel follows the PSR 4 when it comes to autoloading, you can check it from here

Laravel 5.3 Route::resource without REST

I upgrade Laravel 5.1 to 5.3 and have some problem with routes.
In Laravel 5.1 I have route like:
Route::controllers([
'pages/{page_type}' => 'Admin\AdminPagesController',
]);
And in controller I have methods like:
getIndex($type)
postIndex($type, Request $request)
getAdd($type)
postAdd(Request $request)
getEdit($type, $id)
postEdit(Request $request, $id) and others...
But in 5.3 when I created routes:
Route::resource('pages/{page_type}', 'Admin\AdminPagesController');
I got an error
NotFoundHttpException in RouteCollection.php line 161:
or
Route pattern "/master/pages/{page_type}/{{page_type}}" cannot reference variable name "page_type" more than once.
and it generate me route in RESTful
Can anyone help me?
Thanks.
Since there is not alternative to ::controller you need to create separate route for each action if you don't want to use rest:
Route::get('pages/{page_type}', 'Admin\AdminPagesController#getIndex');
Route::post('pages/{page_type}', 'Admin\AdminPagesController#postIndex');
....
Seems that Route::controllers method has been removed in Laravel 5.2, I can't find it in the documentation since then, and doesn't exist in the Illuminate\Routing\Router.php file in Laravel 5.3
You will have to create each route separately for your case. Or you can simply use the Route::resource method, what do you have against it? You can add extra methods to a resource declaring them before the Route::resource call.

How to route to a controller method in Laravel Moduler development?

I am using "artem-schander/l5-modular": "dev-master" for laravel moduler development.
For example i create an Admin module.
Folder structure is App/Modules/Admin.
So controller related to Admin modules placed under App/Modules/Admin/Controllers/ directory.
All routes related to Admin module are placed in App/Modules/Admin/routes.php file.
Here how it looks
Route::group(array('module' => 'Admin', 'middleware' => ['web'],'namespace' => 'App\Modules\Admin\Controllers'), function() {
Route::resource('admin', 'AdminController');
});
All view files related to admin module placed in App/Modules/Admin/Views folder.
I am trying to access Admin's index view using this route
Route::get('/', 'AdminController#index');
This route is place in laravel default routes.php file.
and when i browse ,I am getting this error
Class App\Http\Controllers\AdminController does not exist
From this i understood , laravel looking AdminController in its default path.
How can i overcome this challenge ?
You can access a controller by full qualified namespace if it is not in default path.
Try:
Route::resource('admin', '\App\Modules\Admin\Controllers\AdminController');
I have found two way to do it.
First Option
Changing the $namespace in RouteServiceProvider.php .
For me
private $namespace='\App\Modules';
So for Admin module i can use route as
Route::get('/', 'Admin\Controllers\AdminController#index');
I think this is bad idea to change Laravel's default value.
Second Option
Providing full path of the Controller.
So route would be like this
Route::get('/','\App\Modules\Admin\Controllers\AdminController#index');

Resources