Codeigniter 4 - multiple applications - codeigniter

With Codeigniter version 3 - you could run multiple applications under one installation. With Codeigniter 4 I can't seem to get it working per instructions: https://codeigniter.com/user_guide/general/managing_apps.html
My server is configured using wamp as follows:
C:/wamp/www
/system
/frontend
app/
public
writable
/backend
app/
public
writable
c:/wamp/www/frontend/public/index.php -> configured as follows:
$pathsPath = realpath(FCPATH . '../frontend/app/Config/Paths.php');
c:/wamp/www/frontend/app/config/paths.php -> configured as follows:
public $systemDirectory = __DIR__ . '/../../system';
public $appDirectory = __DIR__ . 'frontend/app';
I'm not sure if these folders are pointing properly...Any ideas would help.
Thanks,
Jeremiah

I finally resolved the issue through trial and error. ../ goes up one level in directories each time (as I had assumed).
In the Paths.php file I had to removed the default value DIR sample below:
Before:
public $appDirectory = __DIR__ . '/..';
After:
public $appDirectory = '../../frontend/app';

This worked for me following the CI4 documentation.
I created two folders for my two apps, each with copies of /app, /public and /writable. I do not have /tests:
system/
frontend/
/app
/public
/writable
backend/
/app
/public
/writable
Then I had to change systemDirectory in Paths.php from:
public $systemDirectory = __DIR__ . '/../../system';
to:
public $systemDirectory = __DIR__ . '/../../../system';

Related

Laravel Image File path not working on server

I have my laravel application hosted on a server in the root directory as follow:
./Project
I have my images folder inside public folder of the project which reside in public_html with the path as following:
./public_html/project/images
How can i upload image on this path from my controller and also how to retrieve data from there?
what i have tried so far for uploading is:
https://www.mywebsite.com/project/images
but it didn't worked for me. can i have a little help on how to resolve this issue
For inserting data, you can use:
$request->file('image')->store('your-path');
Example:
$name = $request->file('image')->getClientOriginalName();
$path = $request->file('image')->store('public/store/images');
$save = new Photo;
$save->name = $name;
$save->path = $path;
$save->save();
And for retrieving an image:
$getImage = {{asset('images/your-image-name')}}

How to change public folder to public_html in laravel 8?

I wanna deploy my application on shared hosting on Cpanel where the primary document root has public_html but Laravel project public
You have to follow 2 steps to change your application's public folder to public_html then your can deploy it or anything you can do :)
Edit \App\Providers\AppServiceProvider register() method & add this code .
// set the public path to this directory
$this->app->bind('path.public', function() {
return base_path().'/public_html';
});
Open server.php you can see this code
if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
return false;
}
require_once __DIR__.'/public/index.php';
Just Replace it with :
if ($uri !== '/' && file_exists(__DIR__.'/public_html'.$uri)) {
return false;
}
require_once __DIR__.'/public_html/index.php';
Then serve your application with php artisan serve, you also can deploy it on your Cpanel shared hosting where primary document root public_html
You can rename your public directory to whatever you want and tell Laravel to use the current directory path as the public path. Just navigate to the index.php inside the public folder (or whatever you renamed it to) and add the following code after the $app definition:
$app = require_once __DIR__.'/../bootstrap/app.php';
/* *** Add this code: *** */
$app->bind('path.public', function() {
return __DIR__;
});
/* ********************** */
there, idk if you have same problem like me, my cases is i using shared hosting, and i deploy it in my main domain. i place all my files in the root, my problem is the storage:link keep between public, not public_html (because its default by the hosting) so what i need to do i changhe the link using this code :
Before :
'links' => [
public_path('storage') => storage_path('app/public'),
],
After :
'links' => [
app()->basePath('public_html/storage') => storage_path('app/public'),
],
I hope it can help few people :)
Just Rename the "public" folder to "public_html" and it will work.
No changes are required in the code. Tested in Laravel 8.
my two cents :) What helped to me was:
Open LaravelService/vendor/laravel/framework/src/Illuminate/Foundation/Application.php and change publicPath() method to return public_html.
public function publicPath()
{
return $this->basePath.DIRECTORY_SEPARATOR.'public_html';
}
Then if you are using webpack also change the output folder:
const output = 'public_html';
mix.ts('resources/js/web/App.ts', output + '/js/web').setPublicPath(output).react();
This helped to me. Only issue is that it is probably not recommended to change Application.php as it is part of Laravel framework and after updating it, it will be probably erased so you have to put it back.

Laravel 5 Dotenv for specific subdomain

I have a few subdomain in my laravel 5 application, each sub domain have a specific configuration, like mail, nocaptcha, etc.
how to set .env file to work with my-specific subdomain ?
Yes, you can use separate .env files for each subdomain so if you use env variables in your config it will work without great modifications.
Create bootstrap/env.php file with the following content:
<?php
$app->detectEnvironment(function () use ($app) {
if (!isset($_SERVER['HTTP_HOST'])) {
Dotenv::load($app['path.base'], $app->environmentFile());
}
$pos = mb_strpos($_SERVER['HTTP_HOST'], '.');
$prefix = '';
if ($pos) {
$prefix = mb_substr($_SERVER['HTTP_HOST'], 0, $pos);
}
$file = '.' . $prefix . '.env';
if (!file_exists($app['path.base'] . '/' . $file)) {
$file = '.env';
}
Dotenv::load($app['path.base'], $file);
});
Now modify bootstrap/app.php to load your custom env.php file. Just add:
require('env.php');
after
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
Now you can create separate env files for each domains for example if you use testing.app, abc.testing.app and def.testing.app you can have .env file for main domain (and for all subdomains that don't have custom env files) and .abc.env and .def.env files for custom env variables your your subdomains.
The best solution I found is to use .htaccess plus env variables.
In .htaccess add these lines:
<If "%{HTTP_HOST} == 'sub.domain'">
SetEnv APP_DOMAIN sub
</If>
In bootstrap/app.php add after the app initialisation:
//own env directory for separate env files
$app->useEnvironmentPath( realpath(__DIR__ . '/../env/') );
//separate files for each domain (see htaccess)
$app->loadEnvironmentFrom( getenv('APP_DOMAIN') . '.env' );
Create a new directory called "env" in your Laravel root and add your config files as:
"sub1.env",
"sub2.env" ..etc
(Of course you can keep it in your root as is currently, but for many subdomains it's better to move into a directory => looks much cleaner => everyone's happy! :) )
You can’t. Each subdomain will be running in the same environment.
If you want per-subdomain configuration then your best bet is to either create a configuration file in the config directory with each subdomain’s settings, or use a database approach.
I had the same issue, Based on #Marcin's answer I built this one (Works with laravel 5.2.X)
I added in the bootstrap/app.php
if (isset($_SERVER['HTTP_HOST'])) {
$hostArray = explode('.', $_SERVER['HTTP_HOST']);
//if the address is a subdomain and exist the .xxx.env file
$envFile = sprintf('.%s.env', $hostArray[0]);
if (count($hostArray) > 2 && file_exists(sprintf('%s/%s', $app['path.base'], $envFile))) {
$app->loadEnvironmentFrom($envFile);
}
}
after
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
I hope that helps someone
Greetings

How to correctly upload Laravel 5 application to server?

I am new to Laravel 5 and have realized that a lot has changed, I am more familiar with Laravel 4. I just tried uploading my site to a live VPS, I managed to change the URLs in index.php and server.php but I keep getting these errors:
Which makes me believe there was something else I was supposed to change that I did not because these files are indeed there and my application works just fine on my localhost.
Also with the exception of my home page, the rest of the pages say not found when I click on their links.
This is my document structure:
index.php
<?php
require __DIR__.'/../*********/bootstrap/autoload.php';
$app = require_once __DIR__.'/../*********/bootstrap/app.php';
$kernel = $app->make('Illuminate\Contracts\Http\Kernel');
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
server.php
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* #package Laravel
* #author Taylor Otwell <taylorotwell#gmail.com>
*/
$uri = urldecode(
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);
// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' and file_exists(__DIR__.'/../public_html'.$uri))
{
return false;
}
require_once __DIR__.'/../public_html/index.php';
Following step you have must followed to move local to live server in Laravel.
Move all public folder data into root directory
In index.php change path to
require DIR.'/../bootstrap/autoload.php'; to require DIR.'/bootstrap/autoload.php';
AND
$app = require_once DIR.'/../bootstrap/app.php'; to $app = require_once DIR.'/bootstrap/app.php';
and set file permission to 744
In .htaccess file add following code after RewriteEngine On.
RewriteBase /
Change parameter in .env file.

CodeIgniter - Delete file, pathing issue

I have 3 folders in my root, "application", "system", and "uploads". In application/controllers/mycontroller.php I have this line of code.
delete_files("../../uploads/$file_name");
The file does not get deleted and I have tried a number of pathing options like ../ and ../../../ any ideas? Thanks.
Use the FCPATH constant provided to you by CodeIgniter for this.
unlink(FCPATH . '/uploads/' . $filename);
base_url() generates HTTP urls, and cannot be used to generate filesystem paths. This is why you must use one of the CI path constants. They are defined in the front controller file (index.php).
The three ones you would use are:
FCPATH - path to front controller, usually index.php
APPPATH - path to application folder
BASEPATH - path to system folder.
$file_name is a variable. You should concatenate it to your own string in order to execute the function:
delete_files("../../uploads/" . $file_name);
EDIT:
Make sure that this sentence:
echo base_url("uploads/" . $file_name);
Is echoing a valid path. If the answer is YES, try this:
$this->load->helper("url");
delete_files(base_url("uploads/" . $file_name));
Supposing that your "uploads" folder is in your root directory.
EDIT 2:
Using unlink function:
$this->load->helper("url");
unlink(base_url("uploads/" . $file_name));
Try this one.. this just a very simple solution to your problem..
If you notice CI has there on defining of base_path to your directory e.g. in the upload library's config:
$imagePath = './picture/Temporary Profile Picture/';
$config['upload_path'] = $imagePath;
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$this->load->library('upload', $config);
if you notice the upload_path is './picture/Temporary Profile Picture/'
so if you want to delete a file from a directory all you have to do is use unlink() function.
unlink($imagePath . $file_name);
or
#unlink($imagePath . $file_name);
Enjoy..^^
This code was working for me. Try this in your Model or Controller. Change the file path according to yours.
file path -->> project_name/assets/uploads/file_name.jpg
public function delete_file()
{
$file = 'file_name.jpg';
$path = './assets/uploads/'.$file;
unlink($path);
}
You should try this code:
$imagepath = $config['upload_path'];
unlink($imagepath . $images);
or
delete_files($imagepath . $images);
public function deleteContent($id)
{
$this->db->where('Filename',$id);
$this->db->delete('tableName',array('Filename'=>$id));
if (unlink("upload/folderName/".$id))
{
redirect($_SERVER['HTTP_REFERER']);
}
}

Resources