I am trying my hand at creating a sub domain route for a project I am working on and I am able to get the sub domain working. However, the only time it works is if I return some copy directly from the routes.php. When I try to access a controller, it tells me that it's not found, when it's clearly there.
This code:
Route::group(['domain' => 'demo.tk.dev'], function(){
Route::get('/', 'DemoController#demoLanding');
});
Returns me this error:
ReflectionException in Route.php line 280:
Class app\Http\Controllers\DemoController does not exist
But if I do something like this:
Route::group(['domain' => 'demo.tk.dev'], function(){
Route::get('/', function() {
return 'Success';
});
});
Then that works.
I am not seeing anything in the documentation about making any other changes for the Controllers to work with the sub domain. Am I missing something? Any help would be greatly appreciated.
check if you have a DemoController in your Controller folder and check if it has correct namespace ie.
namespace App\Http\Controllers;
Related
Hi everyone I need a solution with laravel project when I try open backend admin with /control its giving me an error "Not Found - The requested resource /control was not found on this server."
When I change the name "/control" to anything like "/control5" or something its working fine but the problem is I use /control at views and other! I am new to laravel I didn't know what the problem was? please help me out with this!
Web.php
Auth::routes();
Route::get('/about', [App\Http\Controllers\AboutController::class,'about']);
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/', [App\Http\Controllers\HomePageController::class,'index']);
Route::get('/listing', [App\Http\Controllers\ListingPageController::class,'index']);
Route::get('/details', [App\Http\Controllers\DetailsPageController::class,'index']);
Route::group(['prefix' => 'control','middleware' => 'auth'],function(){
Route::get('/', [App\Http\Controllers\Control\DashboardController::class,'index'])->name('control');
//Pages
Route::get('/pages', [App\Http\Controllers\Control\PagesController::class,'index']);
Route::get('/pages/add', [App\Http\Controllers\Control\PagesController::class,'create']);
Route::get('/pages/edit', [App\Http\Controllers\Control\PagesController::class,'edit']);
});
DashboardController
namespace App\Http\Controllers\Control;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function index(){
return view('control.dashboard');
}
}
Because, you have a folder named control on /public folder. That error occurs when you create a folder in the public folder with the same name as your route so please change the name of the folder you have put in the public folder so that it has a different name from your route this will probably solve your error
I am using laravel 5.4 and trying to get index page, i am using following routes
Route::get('/',
['as' => 'home_page',
'uses' => 'Controller#index']);
and index function in controller looks like this:
public function index()
{
return view('index');
}
But when I visit mydomain.com, I get a different view than index.blade.php.
and it is fine when I use mydomain.com/? or on my local server.
I have searched everywhere in my code and in a google, but didn't found anything, any help?
ie: let me know if any further information required.
First make sure you are calling the right controller, and this dont have a specific middleware blocking the acess to your index method and index.blade.php is inside view folder.
If all of this is fine try this code on your rotes file:
Route::get('', function () {
return view('index');
})
Try this.
First use the make:controller Artisan command to create a controller file. let's say it is homeController.
php artisan make:controller homeController
Then in the homeController file write your code to get the view.
<?php
namespace App\Http\Controllers;
class homeController extends Controller
{
public function index()
{
return view('index');
}
}
Then define a route to this controller.
Route::get('/', 'homeController#index');
For more information please refer https://laravel.com/docs/5.5/controllers
There was a cached view saved on my server, I used
php artisan cache:clear and it got fixed. Thank you everyone for the support.
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');
});
I'm doing rest api with laravel 4 and I'm getting this error:
Unable to generate a URL for the named route "regions.index"
as such route does not exist.
My router:
Route::group(array('prefix' => 'api/v1'), function(){
Route::get('regions', 'RegionsController#index');
Route::get('regions/{id}', 'RegionsController#getOne');
Route::get('regions/{id}/cities', 'RegionsController#getCities');
});
api/v1/regions and api/v1/regions/1/cities
are working fine
but api/v1/regions/1 is not working and thows exception, I really don't know why and can't find how to make it work.
You have not named your routes.
You should change
Route::get('regions', 'RegionsController#index');
to
Route::get('regions', array('as'=>'regions.index', 'uses'=> 'RegionsController#index');
Thank's for answer, I did solve it. I'm stupid and I call in controller route which I previously deleted. :(
I called in controller:
return View::make('regions.index', compact('regions'));
and regions.index was deleted
How can I create admin specific routes in Laravel 4 (Restfull Controllers):
/admin/users (get - /admin/users/index)
/admin/users/create (get)
/admin/users/store (post)
I want to know:
What Files and where I need create theam
How I need create the route
In Laravel 4 you can now use prefix:
Route::group(['prefix' => 'admin'], function() {
Route::get('/', 'AdminController#home');
Route::get('posts', 'AdminController#showPosts');
Route::get('another', function() {
return 'Another routing';
});
Route::get('foo', function() {
return Response::make('BARRRRR', 200);
});
Route::get('bazz', function() {
return View::make('bazztemplate');
});
});
For your subfolders, as I answer here "route-to-controller-in-subfolder-not-working-in-laravel-4", seems to have no "friendly" solution in this laravel 4 beta.
#Aran, if you make it working fine, please add an code sample of your controller, route, and composer.json files :
Route::resource('admin/users', 'admin.Users');
or
Route::resource('admin', 'admin.Users');
thanks
Really useful tool that you can use is the artisan CLI.
Using this you'll be able to generate the needed function file with all the required routes for it to become RESTful.
php artisan controller:make users
Would generate the function file for you. Then in your routes.php file you can simply add
Route::resource('users', 'Users');
This'll setup all the necessary routes.
For more information on this you should read the documentation at.
http://four.laravel.com/docs/routing#resource-controllers
http://four.laravel.com/docs/artisan
Edit:
To make this admin specific, simple alter the code like follows and move the controller to a admin folder inside the controllers folder.
Route::resource('admin/users', 'admin.Users');
The first paramater is the route, the second is the controller filename/folder.
In Laravel if you placed a controller inside a folder, to specific it in a route or URL you'd use the a dot for folders.
You can then expand on this and add Authentication using Route Filters and specifically the code found "Pattern Based Filters" found on the page below.
http://four.laravel.com/docs/routing#route-filters
Laravel 4 - Add Admin Controller Easily
This was driving me insane for ages, but i worked it out.
routes.php
Route::resource('admin', 'Admin_HomeController#showIndex');
/controllers/Admin/HomeController.php
Notice the folder name Admin must be captital 'A'
<?php
class Admin_HomeController extends Controller {
public function showIndex() {
return 'Yes it works!';
}
}
Alternatively you can use the group method
Route::group(array('prefix' => 'admin'), function() {
Route::get('/', 'Admin_HomeController#showIndex');
});
Thanks
Daniel