How to group Controller using route namespace Laravel - php

I am working on laravel project i have completed my admin panel and my all conrollers are in namespace App\Http\Controllers and everything is working perfectly fine now but i want to move all my Controller in folder AdminController for making everything more clear and smooth(Kind of HMVC technique).For this I add namespace on route which I did something like this
Route::group(['prefix' =>'admin','namespace'=>'AdminController'], function ()
{
/// all route
});
and i move all my conrollers in folder AdminController.Now its giving me error Cannot redeclare class Chemist\Http\Controllers\RoleController
Note: My baseconroller are in director App\Http\Controllers and all other controller are in App\Http\Controllers\AdminController

Create custom controller dir like
php artisan make:controller subDriectory/YourController
php artisan make:controller subDriectory/YourController --plain

Related

How to create controller inside folder app but outside folder http in laravel?

I have a folder Web inside folder app of Laravel. And i want to create Controller inside folder Web. But when i run commandline :
$ php artisan make:controller app/Web/Controllers/Webcontroller
It create controller inside folder Http/Controllers as default. So how to do that?
You could try this:
php artisan make:controller ../../App/Web/Controllers/WebController
or create the controller manually as Jerodev suggests in the comments of the question.
What laravel suggest is
It is very important to note that we did not need to specify the full controller namespace when defining the controller route. Since the RouteServiceProvider loads your route files within a route group that contains the namespace, we only specified the portion of the class name that comes after the App\Http\Controllers portion of the namespace.
If you choose to nest your controllers deeper into the App\Http\Controllers directory, use the specific class name relative to the App\Http\Controllers root namespace. So, if your full controller class is App\Http\Controllers\Photos\AdminController, you should register routes to the controller like so:
Route::get('foo', 'Photos\AdminController#method');
so if you create your controller out side Controllers directory you may have to do extra work for it to work.

controller in Laravel

I trying to learn Laravel but I have a problem that I don't understand.
I get an error message when I want to run the controller.
I enter 3 lines of command on CMD
php artisan make:controller WelcomeController
php artisan make:controller AboutController
php artisan serve
Then, I establish the link between the controller and the route.
Route::get('about', 'AboutController#index');
And in the file AboutController.php I have
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AboutController extends Controller
{
public function index()
{
return view('about');
}
}
Normally it should work ? I looked a tuto on youtube https://www.youtube.com/watch?v=QASe8bXMMFA&t=56s
I have as error message => "include(C:\wamp64\www\testprojet\vendor\composer/../../app/Http/Controllers/Controller.php): failed to open stream: No such file or directory"
Do you have an idea please ?
Your error indicates that the Controller.php that your AboutController extends.
Is there a Controller.php file in C:\wamp64\www\testprojet\app\Http\Controllers?
I suspect there isn't, and if needed you can create it using the current Controller.php file in the Laravel repository.
I'm unsure why this is the case, but suspect it may be due to a step being missing when setting your project up... What steps did you follow to get Laravel up and running for your project?
The error might be due to I have faced the same error because I have moved the Controller.php file from http/controllers folder to other place by mistake.
you should check that Controller.php file exists in the app/http/controllers.

Laravel: Routing & Controller

I'm a beginner using this laravel framework. Currently i'm trying to understand the routing and controller of this framework.
I created a controller file using this command:
php artisan make:controller Admin/PostController
Of course the output of this is to create a controller file inside Admin folder. Inside of the PostController.php i wrote a code like this:
public function create()
{
return view('admin.post.post');
}
Also, in my web.php i have this code.
Route::get('/', function () {
return view('user.blog');
});
Route::get('posts',function(){
return view('user.posts');
})->name('posts');
Route::resource('admin/post','Admin\PostController');
The "admin" is a folder and the "post" is a folder too inside the "admin" and when you open the "post" folder you'll see the "post.blade.php" file. The other two route::get in my web.php are working fine. But the route:resource is not working.
When i tried to run this in my browser using this link:
localhost:8000/admin/post/create
The browser says: Sorry, the page you are looking for could not be found
Is there any problem with syntax or path structures?
Here's my post.blade.php
#extends('admin.layouts.app')
#section('main-content')
this is just html codes..
#endsection
Here's my php artisan route:list
this because some time composer stuck, for that times you should stop serve and re generate autoload
1.stop your php artisan serve
2.enter this command
composer dumpautoload
3.run your php artisan serve

Laravel Controller not adding to route list

I created a new controller with resources using the PHP artisan command:
php artisan make:controller AdminCategoriesController --resource
The file gets added to the controller folder but when I visit my route list with I see no route about it:
php artisan route:list
Here my routes list:
How can I add the categories to my routing list?
This error is showing because you did not give instance of Resource Controller in web.php file.
Route::resource('route','AdminCategoriesController ');
When you did this , route list will show.
I failed to add the route resource in my routes.php
Route::resource('/admin/categories', 'AdminCategoriesController');
Everything works now.
If you are using api then use this syntax to generate controller
php artisan make:controller API/PhotoController --api

The HomeController default on laravel doesn't exist

I am new on laravel, and I am making some test and following a tutorial. The matter is that I don´t know what I did two days ago that I got any defaults controllers (maybe only one... I don´t remember, for example: HomeController). I removed the project and create a new one... but now these defaults controllers don´t exit. And my routes.php on app/http folder is like this:
Route::get('/', function () {
return view('welcome');
});
ONLY THIS!!!
I remember that the routes.php file of the first laravel test project had something like: get("home")... or get("login"), etc...
Do I need to install them via artisan or something?
When installing Laravel, i. e. with this command composer create-project --prefer-dist laravel/laravel blog, there is no app/Http/Controller/HomeController.php generated.
You only get one route in your routes.php. That's it!
What you could do, of course, is:
Create a class HomeController.php in app/Htpp/Controllers - use php artisan make:controller HomeController
Change the route in your routes.php to utilize the new HomeController
Route::get('/', 'HomeController#index');
Another thing you may did in the past was running php artisan make:auth to initialize basic controller ands views to get a scaffold for logins/registers of users.
Maybe authentication? It will create some views and routes.
https://laravel.com/docs/5.2/authentication#authentication-quickstart
The command php artisan make:auth if you are using 5.2.*
no you don't if you want to create a controller just tphp artisan make:controller HomeController and a controller will be created for you :)

Categories