Laravel Spark controller routes - laravel

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.

Related

Codeigniter 4 - controllers won't work unless I add them directly to Routes.php

Can anyone tell me:
Do I have to declare all of my controllers in Routes.php in Codeigniter 4?
I can't seem to get a controller to work unless I add it directly to the "Routes.php"
I have created my controllers properly and the Home controller is working after install and setup.
If I add the controller My_page.php :
<?php
namespace App\Controllers;
class My_page extends BaseController{
public function index(){
echo "Controller 'My_page' -> function index() ";
}
}
?>
I get a
: "404 - File Not Found
Sorry! Cannot seem to find the page you were looking for."
If I now add the controller to the rout - i.e.:
$routes->post('my_page', 'My_page::index');
Then my controller works properly and I get the "Controller 'My_page' -> function index() " when I visit www.mydomain.com/my_page
I have also tested:
www.mydomain.com/index.php/my_page
and this makes no difference.
I am using the .htaccess that comes with the download. I have updated the base URL to www.mydomain.com/
The documentation is confusing to me - https://www.codeigniter.com/user_guide/incoming/routing.html#setting-routing-rules ;
it sounds like they are saying have to declare all classes with routes?
Why are my controllers not working without declaring them specifically in Routes.php?
And am I misunderstanding 'setAutoRoute(true)' it also does not seem to work - I expect that I can turn this on and simply create my controllers pretty much like in CI3?
If you don't enable auto-routing you most certainly need to add all routes which you are allowing, anything else will fail with error 404. As #parttimeturtle mentioned - autoroute it is disabled by default since 4.2.
So in short - Yes, you need to add all controllers, their functions and the appropriate http methods. (That includes CLI routes as well)
You can use $route->add(), which will allow all http methods, it's however more secure to explicitly set them with their methods.

How to use old Laravel routing style in Laravel 8

I just installed Laravel 8 and in this version, I have to type my routes like this:
Route::get('/admin/panel', [App\Http\Controllers\Admin\PanelController::class, 'index']);
But I got used to Laravel 5 routes which looked like this:
Route::namespace('Admin')->prefix('admin')->group(function () {
Route::get('/panel', 'Admin/PanelController#index');
});
So how can I use this Laravel 5 routing inside Laravel 8 version?
If you're wanting to continue using the "older" way of defining a route (i.e. Controller#action) then you can do so but you need to alter the RouteServiceProvider to include the App\Http\Controllers namespace.
This is pretty straight forward and is with the more recent versions of Laravel 8 a simple case of uncommenting the following line:
protected $namespace = 'App\\Http\\Controllers';
If the version of Laravel 8 you're using doesn't have this line in the RouteServiceProvider file, you could upgrade your Laravel version or manually add it. If you manually add the line, you will also need to update the Route definitions in the boot method to use the $namespace property. Again, this is very straight forward, just add the following to the web and api definitions:
->namespace($this->namespace)
So for example:
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
Then you should be good to go.
You can still use it to some extend (e.g. grouping and prefixing a route) but because you're using a FQCN the namespace is redundant. The major advantage using the "new" routing over of the "old" one is refactoring. When you move or rename your controller with an IDE that supports refactoring like PhpStorm you wouldn't need to manually change the name and group namespace of your controller.
In Laravel 5 you were able to use this route notation too but it wasn't outlined in the documentation.
To achieve a similar feeling, import the namespace and use just your class names and get rid of the namespace-group in the definition.
use App\Http\Controllers\Admin\PanelController;
Route::prefix('admin')->group(function () {
Route::get('panel', [PanelController::class, 'index']);
});
If you just cannot live with the new way of defining routes, uncomment the $namespace property in app/Providers/RouteServiceProvider.php and you're back to the old way.

Managing Multiple API versions in Laravel

I'm working on exposing an API using one base URL (e.g. https://api.domain.com) and having that URL handle all versions of the API (the API consumer will need to send Accept-Version in the request header to indicate which version of the API they're trying to use).
How would I manage this approach in Laravel?
I was thinking that I could put all of my controllers, helpers, models, routes, config, etc. in a, say, 1.0.0 folder and use those for version 1 of my API. When I release a new version, maybe I make copies of the original code, put them in a 1.1.0 folder, and make changes there.
If I were to use this folder approach, what would I need to do in the routes to indicate which folder to use? How would my controllers know what models, helpers, config, etc. to use? Feels like this approach could get very messy and convoluted.
In your RouteServiceProvider.php you can define the routes for the application. Here you can also define the namespace, name prefix, and middleware for the routes. Simply add a new route map for each version of your api. Have the middleware check the version, redirecting if necessary, and set the name prefix/namespace to the correct version directory.
the RouteServiceProvider would look something like:
protected function mapApi-1-0-0-Routes()
{
Route::middleware(['api', 'version'])
->name('1.0.0.')
->namespace('1.0.0')
->group(base_path('routes/api.php'));
}
then your middleware would look something like
public function handle($request, Closure $next)
{
switch ($request->version) {
case "1.0.0":
$route = $request->version.$request->route()->getName();
break;
// more versions
return redirect()->route($route)->with($request);
}
I haven't tested this.
Route group name prefixes:
https://laravel.com/docs/7.x/routing#route-group-name-prefixes
Route namespaces:
https://laravel.com/docs/7.x/routing#route-group-namespaces
Redirecting named routes:
https://laravel.com/docs/7.x/redirects#redirecting-named-routes
Global middleware:
https://laravel.com/docs/7.x/middleware#global-middleware
Writing service providers:
https://laravel.com/docs/7.x/providers#writing-service-providers

Routes for accessing controllers inside vendor folder of 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.

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