Laravel 5.2 bind controllers to abstract route - php

I am upgrading application written in Laravel 4.2 to Laravel 5.2.
I have long namespaces for my controllers and want to shorten them.
For example:
CustomServiceProvider.php
public function register()
{
$this->app->bind('Shortname\Somecontroller', 'Really\Really\Long\Name\Somecontroller');
}
routes.php
$router->get('someroute', ['uses' => 'Shortname\Somecontroller#someFunction']);
was possible inside custom service provider and works in Laravel 4.2 and Larave 5.1. After I've started migrating to Laravel 5.2 it stopped working.
Closest I came to the solution was this issue report: https://github.com/laravel/framework/issues/14920 but it doesn't work either.
This works for example:
routes_alternative.php
$router->get('someroute', ['uses' => 'Really\Really\Long\Name\Somecontroller#someFunction']);
I've tried Really\Really\Long\Name\Somecontroller::class and moving everything to RouteServiceProvider. I've also tried artisan route:clear,artisan cache:clear, artisan dump-autoload, artisan clear-compiled and all sorts of other composer and artisan shenanigans.
All actions result in exception:
ReflectionException in Route.php line 280:
Class Shortname\Somecontroller does not exist
Does someone have similar expiriences and was able to solve it?

Related

Laravel (ver 6) php artisan route:list return "Target class does not exist" error with production environment (worked fine in local environment)

I'm using laravel 6 for BE and vuejs for FE. Wtih my local environment ( built on xampp server ), everything was fine. But, when I deployed the app to the server(Lamp server with ubuntu 18.04), I got the following error when I tried to run "php artisan route:list".
Illuminate\Contracts\Container\BindingResolutionException : Target class [App\Http\Controllers\Masterdata\CountryController] does not exist.
at /var/www/html/performance.goautobot.chat/public_html/performance/vendor/laravel/framework/src/Illuminate/Container/Container.php:806
try {
$reflector = new ReflectionClass($concrete);
} catch (ReflectionException $e) {
throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
}
And some APIs are not working. The weird part is both "Roles" and "Countries" have exactly the same structure as the following but only apis related to "Roles" are working got "BindingResolutionException" error when I tried with Postman.
Route::group(['middleware' => 'auth:api'], function () {
// Countries
Route::get('masterdata/countries', 'Masterdata\CountryController#index');
// Roles
Route::get('masterdata/roles', 'Masterdata\RoleController#index');
}
I tried to solve it by running the commands like "composer dump-autoload" and "php artisan config:cache" but it didn't fix the problem. Also, tried to remove the project folder and made a new installation and deployment.
Please let me know how to fix the issue. Thank you so much in advance.
I had a similar issue with HomeController, then I checked the routes in the web.php file,
I found it was like the following
Route::get('/', 'HomeController#index');
Then I changed this to the following according to the directory where the Homecontoller resides
Route::get('/', 'App\Http\Controllers\HomeController#index');
after that, it worked correctly.
So Please check your controller directory,
if your controller directory is "App\Http\Controllers\Masterdata\CountryController" then add this in the route. it will be like following
Route::get('/', 'App\Http\Controllers\Masterdata\CountryController');
I tried to explain according to my scenario,
Please let me know if this helps and please pardon me if I have got something wrong

Call to undefined method Illuminate\Routing\RouteFileRegistrar::get() - Error after upgrading from Laravel 5.7 to 5.8

I have a running app written on Laravel 5.7. I tried to change the record in composer.json to match "5.8.*" and ran composer update. On my local (win10/WAMP) machine it went fine, but on the staging server (Debian 9/nginx) the update command changed the vendor contents and failed at the end.
Since then anything I do with the app on the server I get this error and I can't find any information anywhere.
Call to undefined method Illuminate\Routing\RouteFileRegistrar::get()
And this is the line that fails:
$this->get('login', 'Auth\LoginController#showLoginForm')->name('login');
Thanks in advance!
remove "$this" from your routes and use "Route::"
It is a problem with the routes. Mainly, you get this problem when you are using routes with resource or resources. Make sure you do not have any problem in routes by using the command:
#php artisan route:list
If you are getting any problem while route listing please fix it.
I solved this problem in Laravel 5.8 by fixing routes.
Hope this will help.

laravel 5.3 aliases not found => undefined class

I created a project in laravel 5.2, and moving this to laravel 5.3
For this, I created a brand new laravel 5.3 project folder where I am copying views and controllers to the new project. When trying to get Auth working, I see a lot of "undefined class" errors.
By checking laravel documentation, I saw this, where I am stuck now
https://laravel.com/docs/5.3/authentication#retrieving-the-authenticated-user
so, in app.php I see
'Auth' => Illuminate\Support\Facades\Auth::class,
when in my controller, I get "Undefined class Auth" on this
use Auth;
this "undefined class" is solved by changing that line to
use Illuminate\Support\Facades\Auth;
first thing, why can't I just use "use Auth;" ?
In my controller file, I get "undefined class" error on DB, Auth and File
use DB;
use Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Http\Request;
use File;
in my app.php file, these names are aliased as
'App' => Illuminate\Support\Facades\App::class,
'DB' => Illuminate\Support\Facades\DB::class,
'Auth' => Illuminate\Support\Facades\Auth::class,
so I wonder what could be wrong with the aliases ?
Next to this, when trying to get the authenticated user, as described in the documentation, this line is enough
Auth::user()
Here, "user" throws this error:
Method 'user' not found in \Illuminate\Support\Facades\Auth
I checked around using google, comparing files in my Laravel 5.2 against my Laravel 5.3 folders, but did not see any solutions. Maybe someone here has the answer? It worked in my Laravel 5.2 project as described in the link above.
thank you
UPDATE
just try to rebuild the project by these commands in this order:
composer update
php artisan config:clear
composer dump-autoload
but still no progress

laravel 5.1 Route::post not working randomly

When I left work yesterday the routeing worked perfectly. I haven't touched it since, and the only thing I did after I tested the site (and it worked) was make a new model and migration using php artisan make:model.
When I came in to work, about 35 minuets ago, poof. site doesn't work. the routes work with a get method (Route::get('/', 'blahContoller#index') but fails on ALL the Route::post methods. I'm stumped. Going back to laravel 5 after this project, 5.1 is unbelievably broken and worse then 5.0 and has casued me no end of problems that actually wastes more time then laravel is supposed to save.
The error I get:
NotFoundHttpException in RouteCollection.php line 143
my routes:
Route::get('/', 'TurboController#index');
Route::post('checkout', 'TurboController#checkout');
Route::post('securePayment', 'TurboController#securePayment');
Route::get('thankYou', function(){
return view('thankYou');
});
I dont understand, I havent touched the route file since yesterday at 9:00am and worked up until I tested it today.
I HAVE TRYED DUMPAUTOLOAD before you ask ;)
try to run these in terminal
php artisan clear-compiled, php artisan cache:clear, php artisan route:clear

Lumen route caching is not working

When I am trying to run php artisan route:cache in lumen project, I am getting following error,
[invalid argument exception]
There is not commend defined in the 'route' namespace.
My route file is,
$app->get('/', 'App\Http\Controllers\WebController#index');
Any idea?
This command exists only in Laravel and in Lumen it does not. it is not needed as Lumen is anyway optimized for best performance.
Type php artisan to see all possible commands in Lumen.

Categories