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')
Related
I need to load a different .env file, named .env.test under certain conditions.
I attempted to do that through a middleware by adding
app()->loadEnvironmentFrom('.env.test');
Dotenv::create(base_path(), '.env.test')->overload();
to the bootstrap() method of Kernel.php. I also tried to create a dedicated middleware for this and load it as the first one in the web middleware group. But either way, it is loading the standard .env file.
It works if I do it in the /bootstrap/app.php file but I really don't want to put it there.
I just figured it out: The default .env file is being loaded inside of the bootstrap() method of LoadEnvironmentVariables.php.
To use the .env.test file I had to restructure my initial bootstrap() method inside of the App/Http/Kernel.php file to look like this:
public function bootstrap()
{
app()->loadEnvironmentFrom('.env.test');
parent::bootstrap();
}
So the essential part was to move the parent::bootstrap() call below the loadEnvironmentFrom() call.
Instead of doing any code change, you can use export command create a file called .env.test, you want to sue this as .env file use terminal
APP_ENV=local
php artisan config:cache
php artisan key:generate
This below edit is to explain how .env file is getting set
In Illuminate\Foundation\Application class has method loadEnvironmentFrom which is taking the file as parameter and setting it,
you can use bootstrap/app.php after you are getting $app
$app = new Illuminate\Foundation\Application(
$_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);
here you will be having instance of Illuminate\Foundation\Application
you can just call the loadEnvironmentFrom function like
$app->loadEnvironmentFrom('.env.local');
May be it is better to use Kernel.php instead of this, I do not think either of bootstrap/app.php or kernel.php will get overridden with composer update, so make more calculation while using it. I have added this, so that it will help you understand the stuffs.
You can load a different environment file using APP_ENV
For example if APP_ENV=test then .env.test can be loaded.
More info: https://github.com/laravel/framework/blob/6.x/src/Illuminate/Foundation/Bootstrap/LoadEnvironmentVariables.php#L41
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';
});
I have a lot of routes in my project and I don't store all web routes in single file, I use subdirectories.
For example I have standard routes/web.php when I wrote main routes and in this directory I created subdirectory routes/groups. There I have several files separate by some categories. It works fine when I don't use route cache. But if I try create cache by php artisan route:cache it creates cache only for standard files like: routes/web.php, routes/channels.php etc.
Even I add require_once on the web.php it doesn't work!
What the problem with route cache?
Laravel version: 5.6
Proper way to define routes in a subdirectory is to do it in RouteServiceProvider.php file. php artisan route:cache will consider these routes then.
In your case, add the following script inside mapWebRoutes() function should work:
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/groups/{your-filename}.php'));
You can also assign separate prefix, namespace, assign middleware to every routes file. For instance:
Route::middleware('web')
->namespace($this->namespace)
->prefix('groups')
->group(base_path('routes/groups/{your-filename}.php'));
NOTE:
route:cache won't work with closure based routes. So you have to avoid it in order to use route:cache.
I'm experimenting with hyn/laravel-tenancy package. I am amazed how awesome it is, but I've run into issue.
$this->get('login', 'Auth\LoginController#showLoginForm')->name('login'); causes error Class Auth\LoginController does not exist. Same with Auth::routes().
But $this->get('login', 'App\Http\Controllers\Auth\LoginController#showLoginForm')->name('login'); works fine.
How do I fix this to use it as a web.php routing file?
I am using Ubuntu 16.04 and installed Laravel 5.3 on desktop/Laravel/test directory. I have created a test.blade.php file in resources/view directory. I have also created routes.php in app/Http directory and added following code:
Route::get('/', function(){
return view('test');
});
When I enter command in terminal: php artisan serve and go to http://localhost:8000 url in browser, it shows default page of laravel after installation. Why it is not showing view I have created? I have also tried writing following code in routes.php:
Route::get('/', function(){
echo "Test";
})
But still it doesn't work. Is there anything I am missing?
Reference
By default, fresh Laravel 5.3 applications contain two HTTP route
files in a new top-level routes directory. The web and api route files
provide more explicit guidance in how to split the routes for your web
interface and your API.
The routes.php is moved to different folder in Laravel 5.3. Update routes/web.php file.
From the Laravel Documentation 5.3
The routes directory contains all of the route definitions for your application. By default, three route files are included with Laravel: web.php, api.php, and console.php.
The routes.php was there in previous version. But in laravel 5.3 the routes.php is moved to routes/web.php as Saumini Navaratnam said.