Possible to use the default Controller.php in Laravel 8? - php

I added a method to the default app/Http/Controllers/Controller class, and tried to invoke it from my routes/web.php file, but get this error:
Illuminate\Contracts\Container\BindingResolutionException
Target [Illuminate\Routing\Controller] is not instantiable.
Most/all (?) tutorials seem to talk about creating a new controller -- I don't actually want a new controller -- the existing controller seems good enough -- don't want an extra class just to hold a single function.
Can I just use the existing controller?
My route in routes/web.php is:
Route::get('/addarticle', [Controller::class, 'add_article']);
My controller function:
function add_article( Request $request ){
return "hello, from /addarticle controller";
}
Thanks.

Yeah you can use Controller class but you will have to import correct Controller class

It does seem possible to use the default Controller.php class/file -- I was just not importing it into my web.php correctly:
4 //use Illuminate\Routing\Controller; // wrong
5
6 use App\Http\Controllers\Controller; // correct
Thanks!

Related

how to use another controller function without extends in our controller

how to use another controller function without extends in our controller
$this->load->library('../controllers/controllername');
already used
it is giving error =
Unable to locate the specified class: Session.php
Well you are not supposed to do that. If your controller uses repeatable logic, you should make class (Service for example), put the re-usable logic into it and call it in your controllers.
You can't use another controller function inside the controller. You can archive this in these two ways.
Create a Helper class
Create a generic model.

Call to undefined method Illuminate\Support\Facades\App::index()

I'm new to learning Laravel but I'm having trouble routing to controller, I have a controller named "App" and I have a function named index in it, it says it can't find it in "App" controller even though I set it in the route
Error
Error
Call to undefined method Illuminate\Support\Facades\App::index()
http://localhost:8000/anasayfa
App.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class App extends Controller
{
public function index(){
return "anasayfa";
}
}
web.php
Route::get('/anasayfa', 'App#index');
What is the reason for this error?
A class with the name App already exists in Laravel, defined in namespace Illuminate\Support\Facades\App
if you want to use your class make sure to add
use App\Http\Controllers\App
in your web.php
It is recommended to use a different name. You should follow the conventions and name it AppController.
I'm solved.
I deleted the controller named "App" and created a controller named "AppController". But this caused a new error, Laravel could not find class "AppController". For this I updated web.php as follows;
use App\Http\Controllers\AppController;
Route::get('/anasayfa', [AppController::class, 'index']);

PHP Laravel - Controller does not exist

I am new to PHP and Laravel. I am creating my first Laravel API where I am working with Post object.
In order to do so I use a PostController. I have created this controller using the php artisan command: php artisan make:controller PostController --api. This created a PostController class inside the app\Http\Controllers.
When I want to append this controller to the api routes using the Route::resource function I came across something strange. The course I followed told me to declare the controller like so: Route::resource('posts', 'PostController');. When testing the api php artisan route:list this gave the following error:Target class [PostController] does not exist.
Then I did some research and found an other way to declare the controller inside the api routes using:
use App\Http\Controllers\PostController;
Route::resource('posts', PostController::class);
This worked for me but I don't have any clue why the first declaration failed. I have looked inside my PostController class to see any typo's. But I couldn't find any:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
...
}
The reason for this is just like the exception says: it could not find the PostController in your declared namespace.
The reason for that highly depends on the Laravel version you are using. Laravel 8 removed the default namespacing to App\Http\Controllers, so if you are using Laravel 8 but have followed a tutorial for an earlier Laravel version, that might be the reason.
However, using the declaration using class imports is the better way to do it anyways, so I would stick with that.
Since Laravel 8, there were some changes to the automatic controller namespace prefixing.
https://laravel.com/docs/8.x/upgrade#routing
You can continue to use the original auto-prefixed controller routing, see the 4th paragraph on the upgrade documentation. But the new way is recommended.
You should tell Route::resource method the controller's namespace. As you did in your second try.
If you wanna do it with your first attempt, you can tell it the required namespace
Route::resource('posts', 'App\Http\Controllers\PostController');
You can also set the namespace in your App\Providers\RouteServiceProvider:
public function boot()
{
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace('App\Http\Controllers\Api')
->group(base_path('routes/api.php'));
});
}

Can't use $request in controller.index() with lumen / laravel (index should be compatible with App\Http\Controllers\Controller::index )

Using lumen 5.2, I am setting up a GET action that can have resource queries:
GET /expedientes?param1=value1&..paramn=valuen
In my routing I have:
$app->get('expedientes', 'ExpedientesController#index');
In my controller:
class ExpedientesController extends Controller
public function index(Request $request){
if ($request->has('name')) {
return $request->input('name');
}
}
}
The error I obtain:
Declaration of App\Http\Controllers\ExpedientesController::index(Illuminate\Http\Request $request) should be compatible with App\Http\Controllers\Controller::index
The funny thing is, if I rename the controler to a name different from "index", like "indexa" (in both the controller and the routing file) everything works perfectly.
So my question is: Using the standard function name "index" for getting a list of resources, which is the proper way to access the request?
Thanks a lot, regards,
Possible solution #1:
I found out that lumen doesn't use the same controllers as laravel ( lumen: App\Http\Controllers\Controller class not found with fresh install ).
Using
use Laravel\Lumen\Routing\Controller as BaseController;
In the controller so it inherits from lumen's controller
class ExpedientesController extends BaseController
allows me to use a Request parameter in the index method.
But I wonder. Is this the proper way? Even if it is, what would be the way to do this in laravel?

UnexpectedValueException in Route.php line 639: Invalid route action: [App\Http\Controllers\PortfolioController]

Why am I getting this error. I created a PortfolioController. Then I made a route using this
Route::get('portfolio','PortfolioController');
So in my controller page I made this.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class PortfolioController extends Controller
{
//This only gets exectued when we request /portfolio/Paintings using GET
public function getPaintings()
{
return 'This RESTful controller is working!';
}
}
I get this error when typing in localhost/portfolio/paintings
From the look of your code, it looks like you're trying to setup an implicit controller route. You're close, but your route definition is a little off. You need to use controller instead of get:
Route::controller('portfolio','PortfolioController');
https://laravel.com/docs/5.3/upgrade#upgrade-5.3.0
The following features are deprecated in 5.2 and will be removed in the 5.3 release in June 2016:
Implicit controller routes using Route::controller have been deprecated. Please use explicit route registration in your routes file. This will likely be extracted into a package.
You must declare each endpoint now.
try this
Route::get('portfolio','PortfolioController#getPaintings')
I got a similar error when there was a mistake in he file of web.php.
A correct route would like this Route::get('portfolio','YourController#yourMethod');
Use this code in routes:
Route::resource('portfolio','YourController#yourMethod');
you need to explain your function on Route.
example:
Route::methods('your-uri','YourController#YourFunction');
so you should do this:
Route::get('portfolio','PortfolioController#getPaintings');
Hope it helps
You have to consume a function of the controller instead of using the whole controller class for one request. so laravel doesn't know which of your function to use.
Try using PortfolioController#index. or Route::resource('yourroute','PortfolioController');
Try this: Route::resource('/portfolio','PortfolioController');
Hope this will work

Categories