Laravel 5 can't find class for custom namespace - php

In my Laravel app I'm splitting the front end and back end code into folder. These are app/Http/Controllers/BackEnd and app/Http/Controllers/FrontEnd. Rather than type all that out on every file I thought it would be easier to define two namespaces, BackEnd and FrontEnd. I've edited my composer file to this:
"autoload": {
"classmap": [
"app/Models",
"database"
],
"psr-4": {
"App\\": "app/",
"BackEnd\\": "app/Http/Controllers/BackEnd",
"FrontEnd\\": "app/Http/Controllers/FrontEnd"
}
},
I then ran composer autodump and set up my route file like this:
Route::group(['prefix' => 'webman', 'middleware' => 'auth', 'namespace' => 'BackEnd'], function()
{
Route::get('/', ['as' => 'webmanHome', 'uses' => 'HomeController#index']);
});
But when I browse to localhost:8000/webman/ I just get an error, Class App\Http\Controllers\BackEnd\HomeController does not exist. The controller does exist, this is the file:
<?php namespace BackEnd;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class HomeController extends Controller {
/**
* Display the admin home page, showing the front-end menu and "apps" as links to other sections of the ACP.
*
* #param Reqeust $request
*
* #return View
*/
public function index(Request $request)
{
return view('backend.index');
}
}
I've checked vendor/composer/autoload_psr4.php to make sure that the namespace is being defined and it is, this is in the array returned 'BackEnd\\' => array($baseDir . '/app/Http/Controllers/BackEnd'),.
Strangely if I use <?php namespace App\Http\Controllers\BackEnd; at the top of HomeController.php then everything works, why? What am I missing? Why can't autoload find the file with just BackEnd?

When setting namespace in Route::group() it is actually appending that to App\Http\Controllers. What you could do is remove it and reference the full path like so:
Route::group(['prefix' => 'webman', 'middleware' => 'auth'], function()
{
Route::get('/', ['as' => 'webmanHome', 'uses' => '\BackEnd\HomeController#index']);
});

Try changing/commenting the below line in RouteServiceProvider.php
protected $namespace = 'App\Http\Controllers';

There's an interesting and easy way to get around this... Service Providers.
When the route file is loaded via a provider, the 'App\Http...' is not enforced.
public function boot()
{
$this->loadRoutesFrom(app_path('Your/Model/routes.php'));
}
Keep in mind no middleware is attached either. Your route group will have to specify a 'web' middleware or you'll go nuts wondering why validation, etc, isn't working anymore....(been there!)
It's a cool way to go about it anyway, using providers leads to more modular code and re-use.

Related

Laravel 7 Auth not working on hosted site

So I am trying to deploy a Laravel site to bluehost however after successful login which should redirect to '/home' and stop. It instead tries to redirect to '/home' then redirects to '/login'. the same code running on localhost with the same database works fine. Other database operations work fine.
Basically the default auth middleware seems to broken somehow.
I used laravel's built in auth to make make the authentication.
here is the live site:
LoginController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* #var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
}
web.php
<?php
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
Route::get('/', 'WelcomeController#index')->name('welcome');
Auth::routes();
// Home
Route::get('/home', 'HomeController#index')->name('home');
//Listings
Route::get('/listings/search', 'ListingController#search')->name('search-listings');
Route::get('/listings/delete/{listing}', 'ListingController#destroy')->name('listing-delete');
Route::resource('listings', 'ListingController');
Route::group(['prefix' => 'messages'], function () {
Route::get('/', ['as' => 'messages', 'uses' => 'MessageController#index']);
Route::get('create', ['as' => 'messages.create', 'uses' => 'MessageController#create']);
Route::post('/', ['as' => 'messages.store', 'uses' => 'MessageController#store']);
Route::get('{id}', ['as' => 'messages.show', 'uses' => 'MessageController#show']);
Route::put('/', ['as' => 'messages.update', 'uses' => 'MessageController#update']);
Route::delete('/', ['as' => 'messages.action', 'uses' => 'MessageController#action']);
});
So it turns out there was a new line before <?php in one of my files in app/config !!
Almost drove me crazy. Worked fine on localhost but not on server. If you encounter this issue, check your .php files for new lines at the beginning of the file.
For me, deleting the files in /bootstrap/cache folder except the .gitignore
then running the command composer dumpautoload worked

Laravel Admin route not working but others are

I have a problem with my Laravel project. I created an admin route group (domain.co/admin/). The root /admin/ at one point was working then i added a few more pages have updated composer, installed doctrine/dbal, and installed chart.js since then. Now for some reason the /admin/ route no longer works but all other routes work perfectly normal.
my web.php routes look like this:
Route::get('/', function () {
return view('home');
});
Auth::routes(); // tried commenting this out
Route::middleware(['web','auth','rolecheck:Super'])->prefix('admin')-
>group(function(){
Route::get('/', 'AdminController#index');
Route::get('/test', 'AdminController#index');
Route::get('/test2', 'AdminController#test');
....
});
...
There are more route groups that also work
/admin/ gives me a permission error. /admin/test/ /admin/test2/ work fine
here is the controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AdminController extends Controller
{
public function index(){
echo '2';
//return view('admin.dashboard');
}
public function test(){
echo '1';
}
}
.htaccess doesnt show anything weird (default from laravel). I have also tried clearing caches.
I found nothing in the /etc/httpd conf files.
I have tired looking through all the code for the word 'admin' and can't find anything that is pointing to why its saying permission denied.
If i change the prefix to 'admins' it works so i am guessing some part of laravel is blocking the admin/ route. Where else can i look to see where its being blocked.
Check your public folder if you have 'admin' folder there, then rename it. In my case it was a reason of such a behavior.folders structure
Check the config/auth file in your laravel directory and check the guards and providers array in it default look like this.
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
Copy/Paste these files (.htaccess, index.php, favicon.ico, robots.txt) from the /public folder to the /public/admin folder.
Edit the /public/admin/index.php file and add "/.." to all 3 required php files:
from
require __DIR__.'/../vendor/autoload.php';
to
require __DIR__.'/../../vendor/autoload.php';

Laravel package Controller not found in route

I have a simple package and I want to use the controller. When I try to use it in routes I got
Class App\Http\Controllers\Tropicalista\Admin\Controllers\DashboardController
does not exist
I have this in my /routes/web.php
Route::group([
'namespace' => '\Tropicalista\Admin\Controllers',
'prefix'=> 'admin'], function() {
Route::get('/', ['as' => 'admin.root', 'uses' => 'DashboardController#index']);
});
My controller:
namespace Tropicalista\Admin\Controllers;
use Illuminate\Http\Request;
use Analytics;
use Carbon\Carbon;
use Spatie\Analytics\Period;
use Illuminate\Support\Collection;
use Illuminate\Routing\Controller;
class DashboardController extends Controller
{...}
I think is a namespace problem. So how can I call the package controller?
By default, the RouteServiceProvider includes your route files within
a namespace group, allowing you to register controller routes without
specifying the full App\Http\Controllers namespace prefix. So, you
only need to specify the portion of the namespace that comes after the
base App\Http\Controllers namespace.
You need to remove namespace
Route::group(['prefix'=> 'admin'], function() {
Route::get('/', ['as' => 'admin.root', 'uses' => '\Tropicalista\Admin\Controllers\DashboardController#index']);
});
Since it's a package, you need to register the routes in the package.
You can see an example of registering package controllers here:
$routeConfig = [
'namespace' => 'Barryvdh\Debugbar\Controllers',
'prefix' => $this->app['config']->get('debugbar.route_prefix'),
'domain' => $this->app['config']->get('debugbar.route_domain'),
'middleware' => [DebugbarEnabled::class],
];
$this->getRouter()->group($routeConfig, function($router) {
$router->get('open', [
'uses' => 'OpenHandlerController#handle',
'as' => 'debugbar.openhandler',
]);
});
In order to call package controller, change the namespace group of RouteServiceProvider from
protected $namespace = 'App\Http\Controllers';
to null/empty i.e.
protected $namespace = '';
Then, the route can be written as,
Route::get('homepage', 'Package\Namespace\Controllers\ControllerName#ActionName');
Further, if you want to write route for the default controller, use leading slash '/' before starting url.
Route::get('/homepage', 'App\Http\Controllers\ControllerName#ActionName');
Whether it is good practice or not but it solved the problem.

Namespaced routes not working in Laravel 5

I'm building an international website, therefore I managed to have URLs looking like /{language}/{other_stuff} thank to some manipulation in RouteServiceProvider
/**
* Define the routes for the application.
*
* #param \Illuminate\Routing\Router $router
* #return void
*/
public function map(Router $router, Request $request)
{
$locale = $request->segment(1);
$this->app->setLocale($locale);
/**
* Internationalization routing system
*/
$router->group(['namespace' => $this->namespace, 'prefix' => $locale], function($router) use ($locale) {
if ($locale == 'en') require app_path('Http/routes_en.php');
elseif ($locale == 'el') require app_path('Http/routes_el.php');
});
}
Works like a charm. Every language will have his own route file, it's a choice.
Let's say we go to /en/ and you're an admin, I created another namespace within Http/route_en.php to focus on the admin section :
Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function() {
Route::controller('', 'DashboardController');
Route::controller('brands', 'BrandsController');
Route::controller('contents', 'ContentsController');
Route::controller('downloads', 'DownloadsController');
Route::controller('news', 'NewsController');
Route::controller('products', 'ProductsController');
Route::controller('settings', 'SettingsController');
Route::controller('users', 'UsersController');
});
So now I should access easily sections such as /en/admin/brands but it fails. I generate all my links dynamically thanks to the HTML class
{!! HTML::linkAction('Admin\BrandsController#getIndex', 'Brands') !!}
The generation works fine when I go to /en/admin which means Admin\BrandsController#getIndex is detected by this package, but when you click on it
Sorry, the page you are looking for could not be found.
I tested some stuff and when I just simply set the route outside group() it works fine.
Route::controller('admin/brands', 'Admin\BrandsController');
What am I missing here ? Shouldn't the HTML class and the routing system agree with each others ? Is there any mistake I made ? Maybe there's an issue ?
EDIT : I opened an issue for this problem on GitHub
So nobody tried to help me.
After a few days, an issue and many tests I understood the problem by myself : you have to put the DashboardController route at the end otherwise the routing system will take it first and ignore the other ones.
/**
* Admin
*/
Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'middleware' => 'is.admin'], function() {
Route::controller('news', 'NewsController');
Route::controller('brands', 'BrandsController');
Route::controller('products', 'ProductsController');
Route::controller('users', 'UsersController');
Route::controller('downloads', 'DownloadsController');
Route::controller('settings', 'SettingsController');
Route::controller('contents', 'ContentsController');
Route::controller('', 'DashboardController');
});
NOTE : Everything will seem alright in the route listing, and even in the HTML/Form packages, but it's not.
I let it here for anybody that would have similar problems.

Laravel route doesn't work

I am making basic route with controller but it doesnt work, I dont know why... Earyer worked for me but now doesnt...
Here is routes.php:
Route::get('/admin', array(
'as' => 'admin-login',
'uses' => 'AdminController#getAdminLogin'
));
Here is AdminController.php:
class AdminController extends BaseController {
public function getAdminLogin()
{
return View::make('admin.index');
}
}
When I type localhost/project/public/admin... It goes to localhost/admin
Why?
Why don't you use Laravel's own Web Server:
php artisan serve
And then, try this:
http://localhost:8000/admin/

Categories