Laravel: route:cache not use files in subdirectories - 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.

Related

Splitting Laravel / Lumen routes in multiple files

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';
});

is there any method to find out current path of route file in laravel 5.6? [duplicate]

This question already has answers here:
How to clear Laravel route caching on server
(7 answers)
Closed 3 years ago.
I have started working in a existing laravel project, I found all the routes placed in routes/web.php as usual. But When I declare a new route it does not work even not found on artisan route:list command.
So what I've tried so far is finding out how existing routes are working, for that I just deleted some routes from existing routes/web.php file, But I was just wondered finding it has no effect over the project. all the deleted routes still redirects to specified methods of the controllers and all the functionalities are working fine. so it looks like there is another file to place the routes instead of default web.php
I have cleared all the caches like
php artisan config:cache
php artisan cache:clear
php artisan view:clear
all the routes on web.php are as below
Route::group(['middleware' => ['auth']], function() {
//all routes are declared here like
Route::get('/', 'HomeController#index'); //example route
});
So is there any way to find out the route file working behind the scene?
Or is it really possible to change the default path of route file then routes/web.php?
You've cleared a number of caches, but not your route cache.
Try running php artisan route:clear to clear your route cache.
Your routes are cached after you run php artisan route:cache, this is sometimes included in your composer.json scripts so can occur without you even being aware.
There is a file which serves for all routes settings app/Providers/RouteServiceProvider.php. It has method map() where it binds both routes files – web and API:
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
}
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}

Laravel- view is not working

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.

How to rollback effects of php artisan make:auth in laravel 5

I am new to laravel.
I was in middle of my project. I googled for login validation in laravel 5.
I found this command
php artisan make:auth
it Created several classes and also modified my welcome.blade.php
there was several code in welcome.blade.php.
Now How to rollback effects of this command.
Please Help.
Look at the make:auth command source code to understand what exactly files this command added or changed and revert changed back.
manually you have to remove following files
auth/login.blade.php
auth/register.blade.php
auth/passwords/email.blade.php
auth/passwords/reset.blade.php
layouts/app.blade.php
home.blade.php
Go to routes/web.php, delete the created routes by the command make:auth. Remove these two lines and your project will work fine as before.
Auth::routes();
Route::get('/home', 'HomeController#index');
The answer is NO.
There is no way you can rollback make:auth command as yet in laravel..
You can remove auth manually be removing auth from app controller and and auth route. And if you were lucky enough to have welcome file opened in some IDE then there was a chance of ctrl+z bcause IDEs keep back in memory,., but other then that there is no way retrieving backup the data.
we can simply remove below line form wep.php routes folder.
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
you can also remove the view files: like auth folder ,app.blade.php, home.blade.php

laravel 4 workbench routing to the package

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

Categories