Laravel Route to Standalone WebApp - laravel

I am trying to build a portal in Laravel to serve some other, standalone web apps (not built in Laravel), but I am struggling to find out how to route to these apps if I want to place them outside the public folder.
In the past, I would use (temporary) symlinks for this kind of things, but I was wondering if Laravel provides another solution.
So, I have a folder:
module
module/index.php
module/js/whatever
module/css/whatever
module/img/whatever
and I want a route /modules/1 to link to index.php in the module-folder in such a way that the resources in this folder (js/css/img) are also accessible.
Any suggestions?

You can include other PHP files with require_once.
web.php
Route::any('/webapp/{assets?}', 'WebAppController#index');
WebAppController
class WebAppController {
public function index(Request $request) {
require_once '../module/index.php';
if ($request->assets) {
// check from session if user is logged in
// require asset as well
// (or download them https://laravel.com/docs/5.5/responses#file-downloads)
}
}
}
http://php.net/manual/en/function.require-once.php

Related

Path for images for Laravel project from cpanel

I have a problem with displaying pictures in View for my project in Laravel..destination path in my application where the photos were saved is: public_path (/ images) but recently I put the project on the server in cpanel, and to get rid of / public from the url, I used a google article, and I made a folder called laravel outside the public_html folder, in which I keep the application files and in public_html I keep only the files that were in the public folder of the application. In View I display the pictures as follows: "{{asset ('images /'. $ Img-> file_name)}}" .. Only it is no longer displayed. Pictures are saved but no longer displayed. I mention that it is saved in the Laravel folder which is outside the public_html folder..and I think that public / images was created automatically there..I would like to know if anyone has faced such a thing and what is the solution. thank you
I solved the problem like this:
In AppServiceProvider in register() I put this code:
$this->app->bind('path.public', function () {
return base_path('../public_html');
});
and now I have public_html/images and there my images are saved..and the rest of myy app, outside public_html folder in a different folder named laravel_project
In Laravel 8, I solved it adding this to the /app/Providers/AppServiceProvider.php in register method:
public function register()
{
$this->app->bind('path.public', function () {
return base_path('../public_html');
});
}
But in order to create the symlink from the "php artisan storage:link" command, I also had to add this code to /bootstrap/app.php, just after the last singleton:
$app->bind('path.public', function () {
return base_path("../public_html");
});
Finally I used this code to get the image that will be sent to my blade template:
$image=Storage::disk('public')->url('path_to_image');

Laravel deployed to shared hosting not displaying image

I deployed a laravel app to shared hosting (hostinger). Everything is working fine except the images which are not showing up.
I have created a symlink of my storage folder with my public_html folder. Files uploaded enters the public folder but when I link the images they still do not show up.
I have created a symlink to the public_html since I cannot access the public folder. I need help on this please
I need the image to be displayed on the browser
In default Laravel deployment the symlink is created in public folder as storage and links to ../storage/app/public.
In order to debug your problem, please have a look at the developer tools of your browser to find out whether it is a 404 or 403 problem (or even another).
Please check for correct permissions. Otherwise you may change the physical storage path to something inside the public folder. This avoids the requirement to create a symlink. Keep in mind to protect non-public storage via .htaccess for example.
You have to the path to your public assets directory if it is not public directory in the root folder. In your case, the public assets directory is public_html. So please bind the path to it from the root folder.
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
if (App::environment('prod')) {
$this->app->bind('path.public', function() {
return base_path('../../public_html');
});
}
}
}
had the same problem, remember to run 'php artisan config:cache' as per Laravel Deployment guidelines.
Running the command creates a general configuration file
in app/bootstrap/cache/config.php with all settings including directories and links that you should update

Route::resource clean /public from URL

I am making a system using the Laravel 5.6
I uploaded to a server that I have and access this externally
You used the software when a route uses resouce, for example::
Route::resource('materiais', 'MaterialController');
Because the routes you create using Route::get, Route::post, Route::patch, etc.., work correctly
For example, if I'm in
http://192.168.0.23/sisobras/public/neworder
and I go to open a new order he goes to the address
http://192.168.0.23/sisobras/public/serviceorder/2/create?
running correctly, a route looks like this:
Route::get('serviceorder/{id}/create', 'ServiceOrderController#create');
but if I'm in
http://192.168.0.23/sisobras/public/materials
and you can register new material the url looks like this:
http://192.168.0.23/materials/create
The view is called on the controller so
public function create()
{
$units = Units::all();
return view('materials/create', compact('units'));
}
and the lack of sisobras/public/ a Not Found error
When used with the php artisan serve works correctly

Laravel shared project codebase

I've searched a few days now and I don't think I've found a relevant question anywhere.
Is there a way to have multiple "sub-projects/sites" which use a shared codebase for their controllers and models, but have their own routes and views.
For example there would be: public/site1 and public/site2 with their own /views, /routes folder and own .env but would both use the same /app code.
These sub-projects would be all on the same server, so if there would be an other solution where there would be multiple applications which can link controllers and models from 1 main "core" application would be possible too.
EDIT: I'm looking more if there is a way to autoload for example models and controllers from an other location on the server, that way whenever I update the "core" once it gets used on all sub-projects.
You can create directories and namespaces for each subsite you want with a structure like this for example.
app
Http (for shared controllers)
Models (for shared models)
site1
Http
Models
site2
Http
Models
routes
shared.php
site1.php
site2.php
You would need to register them in the composer.json like:
"autoload": {
"psr-4": {
"App\\Shared\\": "app/",
"App\\Site1\\": "site1/",
"App\\Site2\\": "site2/"
}
},
In the RouteServiceProvider you can route each route file using a different domain or subdomain and include the shared.php for each (for auth routes for example). It could look something like:
protected function mapSite1Routes()
{
Route::group([
'domain' => config('sites.site1.domain'),
'namespace' => $this->namespace,
'middleware' => ['web'],
], function () {
require base_path('routes/shared.php');
require base_path('routes/site1.php');
});
}
This way you have one application with shared and seperate sources.
That should get you started :)
I’ve actually done this myself. One my own projects is a multi-tenant content management system. There’s the core CMS codebase, but then for each “tenant” I create a package that contains that tenant’s routes, views, resources etc.
Each package is included via Composer and just has the same directories as any other Laravel project:
/public
/css
/img
/js
/resources
/assets
/sass
/views
/routes
web.php
/src
/Console
/Commands
/Http
/Controllers
PackageNameServiceProvider.php
The service provider class is a bit “hack-y” though. I have to do something like this:
class PackageNameServiceProvider extends ServiceProvider
{
public function boot()
{
if ($this->app->runningInConsole()) {
$this->defineAssetPublishing();
$this->defineConsoleCommands();
}
if ($this->app->make(Tenant::class)->name == 'This Tenant') {
$this->defineRoutes();
$this->defineResources();
}
}
}
It would be nice if I could get rid of the if ($this->app->make(Tenant::class)->name == 'This Tenant') bit. However, it’s working alright for me so far.

Laravel 4 installation on server not working

Hello I have installed laravel 4 on my server, the installation was done from cpanel , I get the default welcome message of Laravel on this link http://www.mysite/laravel/public
I added a controller and a view and I tried to run the url for that new controller and I get a Error 404 - Not Found
The document you are looking for may have been removed or re-named. Please contact the web site owner for further assistance.
This is what I did after laravel was installed :
TestController.php
<?php
class TestController extends BaseController {
public function index()
{
return View::make('index');
}
}
routes.php
Route::get('/Test', function()
{
return View::make('index');
});
views/index.php
<h1>Test</h1>
am I doing something worng?
here is my folder structure :
I had a similar problem when deployed my project on a host the first time.
You should put all the content of your public folder in the pre-existing public or public_html folder and the whole app folder in the root folder of the server.
Then you change app/config/app.php to tell the new path of app to laravel, and change your .htaccess file to point the document root to the new public folder.
This worked for me, if you need additional details, ask.

Resources