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']);
Related
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!
I have laravel 8 project and I am facing issue with controller which is in subfolder.
I have DashboardController located in /app/Http/Controllers/Dashboard. In my web.php I have:
use App\Http\Controllers\Dashboard\DashboardController;
Route::get('dashboard', [DashboardController::class, 'dashboardView']);
DashboardController has this namespace:
namespace App\Http\Controllers\Dashboard;
I have tried uncomment $namespace variable in RouteServiceProvider.php. Also I have added:
->namespace($this->namespace);
in boot() method. But with no luck. I got this error:
Class 'App\Http\Controllers\Dashboard\Controller' not found"
When I have DashboardController in laravel controller folder, everything works well. Also interesting is LoginController. It is in Auth subfolder (Controllers/Auth) and this controller works from subfolder.
Reason why I want to move controller into subfolder(s) is better organisation of files.
Is anybody here, who can help me figure out this issue? Thank you very much in advance.
In that class file you are referencing Controller; most likely extends Controller on the class definition. There is no class named Controller in that namespace you have declared, App\Http\Controllers\Dashboard. You are most likely trying to reference App\Http\Controllers\Controller which means you will need a use statement for that or referencing it by its FQCN, Fully Qualified Class Name.
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'));
});
}
i recive this error
"\Controllers\ProductController does not exist "
but indeed i have this controller in my app/http/controller
class ProductController extends AdminController {
public function index() {
$products=Product::latest()->paginate(20);
return view('admin.product.index',compact('products'));
}
}
.....
Route::get('/admin/product','Productcontroller#index');
maybe check your namespace and see if changing that directory suited with the error will help.
namespace App\Http\Controllers\admin
Directs to the Admin Folder in the Controllers Folder
use App\Http\Controllers\Controller
Uses the main Controller.php file
After all this you can check your routes if they work at this point.
If you want to extend another controller instead of the main controller like in your case, I think you should check the directory of the AdminController.php and use the Use command again if the directory isn't in your namespace directory.
namespace App\Http\Controllers
use App\Http\Controllers\admin\AdminController
Atleast that's where i think the extent of your problem is.
This line:
Route::get('/admin/product','Productcontroller#index');
should probably be (watch the case):
Route::get('/admin/product','ProductController#index');
Also make sure that you import the right namespace: Is your controller really living in App\Http\Controllers namespace ?
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');