Method App\Http\Controllers\LanguagesController::index does not exist - php

LARAVEL
when I make a controller insid folder called "Admin" by command
"php artisan make:controller Admin\ControllerLanguages"
and Route the page This error appears:
Method App\Http\Controllers\LanguagesController::index does not exist.
Bad Method Call Did you mean
App\Http\Controllers\LanguagesController::validate() ?
but when i make the controller normally in the default folder by command:
"php artisan make:controller LanguagesController"
the route runs and the page appears, i want the page appears when i create it in "Admin" Folder, I tried many ways, but to no avail.

You should declare the namespace for the route group
Route::prefix('languages')
->namespace('App\Http\Controllers\Admin')
->group(function() {
Route::get('/', 'LanguagesController#index')->name('admin.languages');
//All other Routes for languages defined here
//LanguagesController is at app/Http/Controllers/Admin folder
});
Or you can import the namespace via use statement like, at the top
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\LanguagesController;
Route::prefix('languages')
->group(function(){
Route::get('/', [LanguagesController::class, 'index'])->name('admin.languages');
//Other languages routes here
});

Related

How can I use resource routes with nested controllers (that have parent models)

I am creating Elequent models with the following structure:
Event->hasMany(Invite)
Invite->belongsTo(Event)
I am creating a controller with the following artisan command:
php artisan make:controller -mInvite -pEvent EventInvitesController --resource
Now if I create custom routes like:
Route::get('event/{event}/invite', 'EventInvitesController#index')->name('event.invites.index');
Route::post('event/{event}/invite', 'EventInvitesController#store')->name('event.invites.store');
Route::get('event/{event}/invite/{invite}', 'EventInvitesController#show')->name('event.invites.show');
Route::put('event/{event}/invite/{invite}', 'EventInvitesController#update')->name('event.invites.update');
Route::delete('event/{event}/invite/{invite}', 'EventInvitesController#destroy')->name('event.invites.destroy');
Route::get('event/{event}/invite/{invite}/edit', 'EventInvitesController#edit')->name('event.invites.edit');
Then everything works fine. But I was wondering if there is a way to do this like
Route::resource('event-invites', 'EventInvitesController');
When I've tried to do this the routes only have a single {event_invites} paramater when I need two.
Is there a way to enable routing for the parent model in Route::resource()?
If not how would I go about extending Route to provide such a method?
Use dot notation:
Route::resource('events.invites', 'EventInvitesController');
This would create a set of routes for posts that include the user identifier. For example:
The ‘index’ route:
http://example.com/events/1/invites
The ‘show’ route: http://example.com/events/1/invites/10
Check out the documentation here:
https://laravel.com/docs/5.1/controllers#restful-nested-resources

Laravel 5.7 what is the Resource Controller route names using some namespaces?

After upgrade to Laravel 5.7,
I was using using route:resource for CRUD in Admin
And the namespace is Admin.
So for example UsersController, the route name for create is
route('admin.users.create')
But It doesn't work for me in Laravel 5.7.
Also I can't use the command
php artisan route:list
because it display an error in connection, although I use a proper connection params in .env file.
Namespaces don't alter route names; they only define a namespace within which the app will search for a controller.
You can add route name prefix ->name('admin.') to your admin route group.
Assign name in routes->web.php: name('admin.users.create');like that.
Route::get('/user', 'UserController#create')->name('admin.users.create');
Then use it in your controller ,view or model
route('admin.users.create').

Laravel controller not found 5.7

I'm working on Laravel 5.7 project and I'm trying to create a controller with this name AdminController with this command "php artisan make:controller foldername\AdminController" then I give it a route like this Route::get('/admin','AdminController#login');
the thing is I can't find the AdminController in my project folders? I searched in App/Http/Controller also it's not there?
You put your controller in a folder so you must call this folder name in Route and in namespace of controller.
Try "php artisan make:controller foldername/AdminController"
Your route must be:
Route::get('/admin','foldername\AdminController#login')
And your controller namespace is namespace App/Http/Controller/foldername
For example, by running the command php artisan make:controller PostController it will create the
PostController.php in app/Htttp/Controllers
And you can access in route:
Route::get('/posts', 'PostController#index')->name('posts.index');
Route::get('/posts/create', 'PostController#create')->name('posts.create');
Route::post('/posts', 'PostController#store')->name('posts.store');
Route::get('/posts/{post}', 'PostController#show')->name('posts.show');
Route::get('/posts/{post}/edit', 'PostController#edit')->name('posts.edit');
Route::put('/posts/{post}', 'PostController#update')->name('posts.update');
Route::delete('/posts/{post}', 'PostController#destroy')->name('posts.destroy');
But in your situation, you are using the custom namespace. For example:
php artisan make:controller Admin\PostController
It will create the new folder inside the Controllers with file:
app/Http/Controller/Admin/PostController.php
Now you can't access for the route like the previous:
Route::get('/posts', 'PostController#index')->name('posts.index');
Or
Route::resource('/posts', 'PostController');
If you are using custom nameSpaces for the large number of controller, try below method:
Route::group(['namespace' => 'Admin'], function () {
Route::resource('/posts', 'PostController');
});
Or:
Route::group(['namespace' => 'Admin'], function ()
{
Route::get('/posts', 'PostController#index')->name('posts.index');
Route::get('/posts/create', 'PostController#create')->name('posts.create');
Route::post('/posts', 'PostController#store')->name('posts.store');
Route::get('/posts/{post}', 'PostController#show')->name('posts.show');
Route::get('/posts/{post}/edit', 'PostController#edit')->name('posts.edit');
Route::put('/posts/{post}', 'PostController#update')->name('posts.update');
Route::delete('/posts/{post}', 'PostController#destroy')->name('posts.destroy');
});

I am unable to access Laravel view

I have just created one of about view in view/about.blade.php, and I am accessing this from localhost/myproject/public/about, but it's not working.
However, localhost/myprojects/public/ is working fine; about view has been created on same parameters as welcome by default in Laravel.
Firstly the information is not sufficient to say anything.Please provide your route.Also its important how you are running your project ,is it via Xampp(or Lampp whatever is there) or "php artisan serve"
but looking from your working directory "localhost/myprojects/public" I guess its not by the command . Try localhost/myprojects/public/about.blade.php or run it by php artisan serve and try route localhost:8000/about
Have you added particular routing to web.php file?
Route::get('about', function () {
return view('about');
});
https://laravel.com/docs/5.7/routing
Which error are you getting?
404 - Not found
Route::get('/about', function () {
return view('about');
});
Check routes
php artisan route:list
Laravel is a MVC Framework, Which means You Have a Controller which procede some logic when some request come in and interact with the model if need, after that the controller return some view.
And because you whan to acccess your view file, you must past through controller, and that controller will render the view. Because the views folder is not in the \public dicretory as subdirectory you can't access to It with url like localhost/myproject/public/about even if you get access to it, you will not get HTML, you'll get some plain text with Blade tags. It's a must to return view in you controller by rendering it, somewhere in the background Laravel procede all Blade Tag and return HTML that correspond to that tags.
What I can suggest you Is to create some route in your route file like this
Route::get('/about', function(Request $request){
// Automatically Laravel will look this file in the view directory
return view('about');
});
Or you can go with the controller like procedure by creating some controller, go in your terminal and execute
php artisan make:controller AboutController
this will generate a file name AboutController.php in app\Http\Controllers diretory within witch you will found
namespace App\Http\Controllers;
class HomeController extends Controller
{
}
after that add
public function index()
{
return View::make('about');
}
Don't forget to include use the Illuminale\Supports\Facades\View on top of your file
And one more important thing which left is to configure the Route, for that go in routes directory in the web.php file add
Route::get('/about', 'AboutController#index')->name('about');

Laravel namespacing routes

I have controllers in different folder than Laravel native App\Http\Controllers. I am using a custom Lib\MyApp folder which has modules inside. Each module has its own controllers, models etc. I added to composer.json autoloading to app\lib.
What I did is change RouteServiceProvider namespace:
protected $namespace = 'App\Lib\MyApp';
I did a composer dump-autoload after everything.
Inside MyApp is a Landing\Controller folder with actual controller class inside.
Try 1 (ideal):
I would like to call my route like this:
Route::get('/', 'Landing\Controller\LandingController#index');
But this way I am getting a ReflectionException that the class is not found even though
Try 2:
Route::get('/', '\Landing\Controller\LandingController#index');
Trailing slash gets rid of the namespace part when I refresh the page, and class is still said not to exist.
Try 3:
Route::get('/', 'MyApp\Landing\Controller\LandingController#index');
This just duplicates MyApp folder, and class is not found as expected.
Try 4 (working, but don't want it like that)
Route::get('/', '\MyApp\Landing\Controller\LandingController#index');
This works fine, although I would like to get rid of the \MyApp\ part.
Is something like this possible?
You can use the namespace in the routes for that purpose :
Route::namespace('Landing\Controller')->group(function () {
Route::get('/', 'LandingController#index');
// + other routes in the same namespace
});
And dont forget to add the namespace to the controllers :
<?php namespace App\Lib\MyApp\Landing\Controller;
PS : in the case where the Lib is inside the App folder there is no need to add a thing in the composer file, because the App folder is registred in the psr-4 and with this it will load all the files within this namespase for you.
There are many ways to add the namespace in Laravel
Route::group(['prefix' => 'prefix','namespace'=>'Admin'], function () {
// your routes with"App\Http\Controllers\Admin" Namespace
});
Route::namespace('Admin')->group(function () {
// your routes with"App\Http\Controllers\Admin" Namespace
});
//single route
Route::namespace('Admin')->get('/todo', 'TaskController#index');
//single route
Route::get('/todo', 'Admin/TaskController#index');
// by ->namespace
Route::prefix('admin')->namespace('Admin')->group(function () {
// route code
});
For "laravel 8"
Here I have given an example with both namespace and prefix but you can also use any one according to your requirement.
I created Controller in Controllers dir with command
php artisan make:controller Admin/StoriesController
Route::namespace('Admin')->prefix('admin')->group(function(){
Route::get('/deleted_stories',
'\App\Http\Controllers\Admin\StoriesController#index')->name
('admin.stories.index');
});

Categories