Routes for accessing controllers inside vendor folder of laravel - laravel

I have LfmController.php of tswaler laravel-filemanager package with folder structure such as project\vendor\tsawler\laravel-filemanager\src\controllers\LfmController.php.
I used route as Route::get('/laravel-filemanager', 'Tsawler\Laravelfilemanager\controllers\LfmController#show'); for accessing that controller's show() method.But it gives:
ReflectionException in Container.php line 737:
Class App\Http\Controllers\Tsawler\Laravelfilemanager\controllers\LfmController does not exist error.This worked in my previous project but it now doesn't work on current project.What should be route structure to access controller inside vendor folder,like above?How to debug this kind of issue?

You should add '\' in the beginning of the controller namespace:
Route::get('/laravel-filemanager', '\Tsawler\Laravelfilemanager\controllers\LfmController#show');
By default, routes.php assumes your controller is in 'App\Http\Controllers' namespace but adding '\' will cause it to look in the root namespace.

In my case I forgot to set 'use_package_routes' to false in the config/lfm.php to enable my custom routes.

Related

Redirection issue in Codeigniter4

I have done admin controller and put that in a sub folder named 'Admin'
Controller
Admin
-login.php
Now I want to fetch that by router file where I wrote this
$routes->get('admin', 'Admin/Login::index');
But it is showing me "Not found" error and redirects to "http://localhost/admin".
Could there be some .htaccess issue?
replace this
$routes->get('admin', 'Admin/Login::index');
with
$routes->get('admin', 'Admin\Login::index');
also make sure you add namespace in your login.php
namespace App\Controllers\Admin;
If you keep CI4's directory structure intact you could in fact use sub-folders for Controllers, Models, Views, etc.
For example app/Controllers/Admin/Login.php is a valid place to put a Controller class. Make sure to add the appropriate namespace in Login.php - namespace App\Controllers\Admin; Also in routes - $routes->get('admin', 'App\Controllers\Admin\Login::index'); It is quite possible to work without the prefix of App\Controllers, but I never extensively tested it and I think there was a problem in some versions of CI4 before.
Another issue could be your app/Config/App.php class. If you did not change anything in your .htaccess file (the one in public directory!), $baseURL should be set to your public directory address - http://localhost/myproject/public/ . Or if you wish to make it easier - set up virtual hosts.
Just a thing to add - get() method in $routes allow only GET requests, meaning if you are trying to POST something (or use any other HTTP request method) it will fail and redirect.

Laravel Spark controller routes

I upgraded my Laravel Spark to version 6 and now I get the error ...
Class App\Http\Controllers\TeamController does not exist
This was not a problem previously, so I took a look at the routes files in /vendor/laravel/spark-aurelius/Http and compared them with previous versions. Up till version 5, the route was ...
$router->get('/'.$pluralTeamString.'', 'TeamController#all');
In version 6, the route is ...
$router->get('/settings/'.Spark::teamsPrefix(), 'TeamController#all');
I tried altering my routes file from this ...
$router->get('/teams', 'TeamController#all');
To several configurations of \settings\TeamController but couldn't get one to work. Am I missing something here?
Here are the pertinent parts of the Team Controller code:
namespace Laravel\Spark\Http\Controllers;
use Laravel\Spark\Spark;
use Illuminate\Http\Request;
use Laravel\Spark\Contracts\Repositories\TeamRepository;
class TeamController extends Controller
{
...
#joshua-foxworth Very likely you are trying to create the route on web.php under the routes folder. We know web.php by default checks for controllers under app -> Http -> Controllers folder. You are looking for this route file here from root dir spark/src/Http/routes.php Hope this solves your problem.

laravel 5 can't find mailjet class in the app folder

I've started learning laravel and try to add mailjet to the RegisterController.php . I have a folder named Mailjet in the app folder with a file named Mailjet.php . This file contains this namespace at the beginning
namespace App\Mailjet;
in the RegisterController
use App\Mailjet\Mailjet;
But I get this error
FatalErrorException in RegisterController.php line 24: Trait 'App\Http\Controllers\Auth\App\Mailjet\Mailjet' not found
While it should point to the mailjet folder in the app folder not in the auth folder. So how to fix this to access mailjet from the login, register controllers?
The issue because i added
use App\Mailjet\Mailjet;
inside the class not before it. Sorry about this.

How to route to a controller method in Laravel Moduler development?

I am using "artem-schander/l5-modular": "dev-master" for laravel moduler development.
For example i create an Admin module.
Folder structure is App/Modules/Admin.
So controller related to Admin modules placed under App/Modules/Admin/Controllers/ directory.
All routes related to Admin module are placed in App/Modules/Admin/routes.php file.
Here how it looks
Route::group(array('module' => 'Admin', 'middleware' => ['web'],'namespace' => 'App\Modules\Admin\Controllers'), function() {
Route::resource('admin', 'AdminController');
});
All view files related to admin module placed in App/Modules/Admin/Views folder.
I am trying to access Admin's index view using this route
Route::get('/', 'AdminController#index');
This route is place in laravel default routes.php file.
and when i browse ,I am getting this error
Class App\Http\Controllers\AdminController does not exist
From this i understood , laravel looking AdminController in its default path.
How can i overcome this challenge ?
You can access a controller by full qualified namespace if it is not in default path.
Try:
Route::resource('admin', '\App\Modules\Admin\Controllers\AdminController');
I have found two way to do it.
First Option
Changing the $namespace in RouteServiceProvider.php .
For me
private $namespace='\App\Modules';
So for Admin module i can use route as
Route::get('/', 'Admin\Controllers\AdminController#index');
I think this is bad idea to change Laravel's default value.
Second Option
Providing full path of the Controller.
So route would be like this
Route::get('/','\App\Modules\Admin\Controllers\AdminController#index');

Laravel 4 class not found error

I'm getting an error that a class (a controller) is not being found. Thing is, it is found in my local development environment. I created the controller with php artisan controller:make CssController --path=app/controllers/home.
I renamed the class (not the file) Home_CssController. I added the route:
Route::get('home/css-php', 'Home_CssController#index');
to my routes file. Everything works fine in my local environment. I did forget to run composer dump-autoload but there were no issues with viewing the controller/view in my local environment. I've uploaded everything to the live server, but I'm getting the error:
Class Home_CssController does not exist
I've uploaded the controller, the routes file and the view multiple times. But I still get the error. Is it because the controller wasn't registered with composer? I've since registered it, but am not sure what I need to upload to the server. I've uploaded both the config and bootstrap folders. What do I need to do to get the controller/view to be found? This is Laravel 4.
If the controller isn't in the app/controllers folder, then it will need to be namespaced (unless you want to continue using your autoload.php trick).
Namespace Home_CssController to Home.
<?php namespace Home;
class CssController extends \BaseController
Then, you can use it in your routes:
Route::get('home/css-php', 'Home\CssController#index');

Resources