how to define laravel 5.0 routes by means of directories - 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');
});
}

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

Laravel 5.4 running all routes through subdomain

I have just deployed Laravel 5.4 using Composer on a shared hosting and I've run into several problems which I think are now fixed.
The first one was folder permissions which is now fixed so at least I see a welcome page.
I noticed that composer install never create a routes directory so I uploaded a local version and also it never creates a cache directory in the boostrap folder which I fixed also.
I have installed Laravel on a subdomain and I want everything to run through the subdomain as the root folder has a different application.
If i got to the subdomain, the welcome page opens as expected but as soon as I try to use a different route, I get this error:
NotFoundHttpException in RouteCollection.php line 145:
I have looked at what other people have done with subdomains but nothing works for me. I don't know if there are other files are missing during the install
This is what I have in the Routes.php
Route::group(['domain' => 'subdomain.example.com'], function () {
Route::get('/', function () {
return view('welcome');
});
Route::get('test', function () {
return view('welcome');
});
});
I assume every route needs to run in the subdomain route group or do I need to use this at all seeing as the install is in the subdomain directory and the vhost is pointing to the subdomain public folder
It turned out to be a problem with the PHP version the command line was using on Plesk.
Currently Plesk runs in 5.4 and Laravel needed 5.6. For some reason Composer ran successfully and it seemed to miss downloading some of the files.
I wiped everything from the subdomain and thanks to this http://blogs.reliablepenguin.com/2015/08/18/using-php-composer-phar-with-non-default-php-install i was able to run composer using php version 5.6
I didn't need any special route for subdomains as everything is in the subdomain and these routes worked as expected
Route::get('/', function () {
return view('welcome');
});
Route::get('test', function () {
return view('welcome');
});

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.

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

Hosting two different applications running on Laravel 4 on shared hosting

I'm about to start development of a site using Laravel4 that will include a cms hosted on a sub-domain. What I want to know if is there is a way to have the main Laravel installation shared between the two apps?
I've had various success in testing using the following example: Laravel full URL routing, however I want to keep the functionality from the app folder separate and have something like say, app_main, app_cms that holds the relevant models, views and controllers for each site in there.
There doesn't seem to be much help that I can find in how to set up Laravel for such a requirement, so any help on this would be great.
I have a multihosting solution, which after logging into FTP contains this folders:
domaina.com
domainb.com
domainc.com
If I'd like to share same Laravel code between those websites,
I just create 'Laravel' folder on the same level, so it looks just like this:
domaina.com
domainb.com
domainc.com
laravel
This 'laravel' folder holds everything except 'app' & 'public' directory.
I just place content of 'public' directory right inside 'domaina.com' folder (for example).
Open up 'index.php' and adjust these lines to match actual location.
Now you are good to go
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/start.php';
bootstrap/paths.php holds the main folder definitions. For each instance, you can alter the app folder, public folder, base folder, storage folder and so on from the file.
You can set a cookie/session, or check domain etc. for main and subdomain and alter it like:
'app' => Session::has('subdomain')?'../../app':__DIR__.'/../app',
use Router for this.
e.g.
Route::group(array('domain'=>'example.com'), function(){
//Define the routes for example.com
});
Route::group(array('domain'=>'cms.example.com'), function(){
//Define the routes for cms.example.com
});
for easier maintanace, you can use namespace for controllers.
e.g. your controller folder can look like this
App
|
|---Controllers
|-- site
|
|-----HomeController.php
|-- subdomain
|
|-----HomeController.php
now, for site controllers, use a namspace like, <?php namespace site;?>
for subdomain controllers, use a namespace like <?php namespace subdomain;?>
in the routes file, define the routes as,
Route::group(array('domain'=>'example.com'), function(){
Route::get('/', array(
'as' => 'index',
'uses' => '\site\HomeController#index'
))
});
for subdomains,
Route::group(array('domain'=>'cms.example.com'), function(){
Route::get('/', array(
'as' => 'cms.index',
'uses' => '\subdomain\HomeController#index'
))
});
plain and simplest way.

Categories