Lumen framework comes with the routes/web.php file. Reading about how to split the routes in multiple files I came across Laravel documentation (not Lumen) and there it seems pretty clear.
#see https://laravel.com/docs/6.x/routing#basic-routing >>> The Default Route Files
It states
All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by the framework. The routes/web.php file defines routes that are for your web interface. ...
Routes defined in the routes/api.php file are nested within a route group by the RouteServiceProvider. Within this group, the /api URI prefix is automatically applied so you do not need to manually apply it to every route in the file. You may modify the prefix and other route group options by modifying your RouteServiceProvider class
So you can just add other route files and edit the app/Providers/RouteServiceProvider.php class, this seems pretty straight and clear.
Just that Lumen doesn't have any app/Providers/RouteServiceProvider.php class
So then what's the best way to define your own route files without mangling the framework?
Thanks!
We can do this just like Laravel.
Make routes directory in root folder.
Inside routes directory create files, for instance,
like routes/users.php, routes/posts.php
Add above route files, in bootstrap/app.php file
// Load The Application Routes
$app->router->group([
'namespace' => 'App\Http\Controllers',
], function ($router) {
require __DIR__.'/../routes/web.php';
require __DIR__.'/../routes/users.php'; // mention file names
require __DIR__.'/../routes/posts.php';
});
The equivalent in Lumen is located in /bootstrap/app.php.
You can add routes file entries there appropriately. As you can see, there isn't really a specific API for adding files or anything. So just write the logic as you see fit.
If your routes are located in another folder like app/Api/V1 and your controller are located in app/Api/V1/Controllers then you can use the below code in bootstrap/app.php
Folder Structure
For route : app->Api->V1->routes.php
For Controller : app->Api->V1->Controllers
Code:
$app->router->group([
'namespace' => 'App\Api\V1\Controllers',
], function ($router) {
require __DIR__.'/../app/Api/V1/routes.php';
});
Related
I have a folder Web inside folder app of Laravel. And i want to create Controller inside folder Web. But when i run commandline :
$ php artisan make:controller app/Web/Controllers/Webcontroller
It create controller inside folder Http/Controllers as default. So how to do that?
You could try this:
php artisan make:controller ../../App/Web/Controllers/WebController
or create the controller manually as Jerodev suggests in the comments of the question.
What laravel suggest is
It is very important to note that we did not need to specify the full controller namespace when defining the controller route. Since the RouteServiceProvider loads your route files within a route group that contains the namespace, we only specified the portion of the class name that comes after the App\Http\Controllers portion of the namespace.
If you choose to nest your controllers deeper into the App\Http\Controllers directory, use the specific class name relative to the App\Http\Controllers root namespace. So, if your full controller class is App\Http\Controllers\Photos\AdminController, you should register routes to the controller like so:
Route::get('foo', 'Photos\AdminController#method');
so if you create your controller out side Controllers directory you may have to do extra work for it to work.
I'm working with the Laravel 5.6 framework and I want to move the routes file to another directory. I moved it to the ~/config/routes/web.php from the original ~/routes/web.php directory.
I then changed the mapWebRoutes method in my RouteServiceProvider to this:
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('config/routes/web.php'));
Now when I try to start up laravel I get the error:
In web.php line 14:
Class 'Route' not found
I already checked the aliases array in my app.php config file. There it's just the right class:
'Route' => Illuminate\Support\Facades\Route::class,
When I change my routes directory back to the original ~/routes/web.php directory, every works.
I also tried to import the class into the web.php routes file like this:
use Illuminate\Support\Facades\Route;
Then I received this error when trying to start up laravel.
In Facade.php line 218:
A facade root has not been set.
How can I make it so that I can move my routes file to another directory without getting this error?
Move them to a directory that isn't being scanned for a specific reason.
The config directory is specifically for config files and they get loaded very early in the process, before the providers ... that is how your application gets the list of providers to load, from the config file.
If you want to make a directory named random in the root of your project and put your routes there, just change the path appropriately in the RouteServiceProvider and you will be good.
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('path/routes/web.php'));
But path can not be config,
For example base_path('public/routes/web.php') but not base_path('config/routes/web.php')
I would like to do this thing with laravel 5.0.
I would have many routes.php files in some folders (like, under the app/ folder, I create a "linux" folder and a "windows" folder). Inside the two linux and windows folders, i put two routes.php files, with different routes (like, i don't know, they both have the route get('os') ).
What I want is that calling public/windows/os it is called the route os of the windows folder, and calling public/linux/os it is called the route os of the linux folder (basic polimorphism).
I searched over and over and found nothing helpful. Do you have some advice?
Thanks.
You can simply include the other route files in your main routes.php and put them inside route groups:
Route::group(['prefix' => 'windows'], function(){
include app_path('windows/routes.php');
});
Route::group(['prefix' => 'linux'], function(){
include app_path('linux/routes.php');
});
To reduce the duplicate code you can also use an array to configure your subdirectories:
$systems = ['windows', 'linux'];
foreach($systems as $os){
Route::group(['prefix' => $os], function(){
include app_path($os.'/routes.php');
});
}
I have been working with laravel 4 for some time now and i needed to create an admin area so i decided to use a package to keep things all organized and separated from the rest of the application.
So i created a package with composer as 'vendor/admin'.
then i added those lines as documemented on laravel site
AdminServiceProvider.php
public function boot()
{
$this->package('vendor/admin', 'admin');
include __DIR__.'/../../routes.php';
}
public function register()
{
//
$this->package('vendor/admin');
}
I also created a routes.php file in vedor/admin/ directory to route all admin area in this file.
following i run the 'php artisan dump-autoload' and i finalized with this commend on artisan 'php artisan config:publish vendor/admin'
now i wanna be able use this package for mysite.com/admin route and i want the routes.php file in the package to render the routing for that URI, to do that:
Do i need to modify my app/routes.php?
How can i make vendor/admin/src/routes.php file to do the routing for all mysite.com/admin routes?
Thanks.
No you don't need to edit app/routes.php. As long as it doesn't contain any admin routes that could collide with the ones in your package you can leave it that way.
The routes file in a package can be used like the "normal" app/routes.php. An easy way to deal with admin routes is to have a prefix group:
Route::group(array('prefix' => 'admin'), function(){
// all your admin routes. for example:
Route::get('dashboard', '...');
// will match GET /admin/dashboard
});
Besides that, make sure you're package gets loaded correctly! One part being registering the service provider. Assuming the namespace of your package is Admin you need to add Admin\AdminServiceProvider to the providers array in app/config/app.php. More information
I have worked on yii framework before and I had possibility to create module folder and inside this put eg: news module which was included controllers, views and module
I'm new in laravel and trying to create same thing MODULE
i tried the following inside routing
Route::get('/news','/modules/news/controllers/NewsController#index');
file exist but i'm getting
ReflectionException
Class /modules/news/controllers/NewsController does not exist
why ? what i'm doing wrong ?
The Route::get() function is looking for a (auto)loaded Class, not for a file on the disk to load, which is why you're getting these errors.
It's more Laravely (Laravelish?) to include:
Controllers in the /app/controllers/ directory
Views in /app/views/ directory
Models in the /app/models/ directory
And if you are starting out with Laravel, this might be the best way to get started. The autoloader knows where to look for your classes then, and everything gets handled automatically for you.
With the NewsController situated in /app/controllers/ you can do this:
// no need to start the route location with a slash:
Route::get('news', array('uses' => 'NewsController#index'));
You can "package" up functionality using Laravel's Packages, but it would be better to check out the excellent docs and then come back with specific questions..
Put your Laravel controllers in app/controllers, since that directory gets autoloaded, and it is where Laravel expects controllers to be.
Then you can use routes like this (example straight from the docs at http://laravel.com/docs/controllers#basic-controllers)
Route::get('user/{id}', 'UserController#showProfile');