• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tutorial first app issues when not installed in doc root


New to CodeIgniter 4, used earlier versions and really liked it.  I'm doing a clean install using Composer and pretty much following the Manual/Tutorial directions.  All seemed well, but having problem with the ci-news demo app and generated URLs.  I see several areas where URLs can be adjusted, but I don't want to fight it and would like a clean solution. 

My setup:
Web server doc root /pub/comwww/soaringtools -  www.soaringtools.org [don't want to change that, existing site]

Tutorial Steps:
Installed in the directory: /pub/comwww/soaringtools/ci-news  using composer and moved dirs as recommended.
  (contents) app/  builds*  composer.json  composer.lock  license.txt  phpunit.xml.dist  public/  README.md  spark*  tests/  vendor/  writable/

Followed the tutorial, updated Routes.php as directed, no changes to Paths.php, no changes to .htaccess in public directory, edited the .env file to set DEVELOPMENT and DB access info, no change to baseURL

What works now:
https://www.soaringtools.org/ci-news/public -- brings up the Welcome to Codeigniter OK
https://www.soaringtools.org/ci-news/public/home or about-- brings up those pages OK
https://www.soaringtools.org/ci-news/public/news -- brings up the news summary page OK,

but "View article" links are wrong. 
   They generate the URL -> https://www.soaringtools.org/ci-news/pub...is-sighted
    instead of -> https://www.soaringtools.org/ci-news/pub...is-sighted    (which does work)
Can this be adjusted/fixed simply?  Do I need to move the public directory?

I want the final URLs to start with: https://www.soaringtools.org/ci-news/new ... (no public)

I see adjustments possible in:
ci-news/public/.htaccess   - use RewriteBase as suggested there, but to what?
ci-news/.env    -  change the value of app.baseURL, to what?
ci-news/app/Config/Paths.php -- change systemDirectory or appDirectory, both?
-- or do I just need to adjust the generated paths in the view directly.

Any help is welcome.  I'm trying to avoid forcing it by using the wrong methods.
Many Thanks!

Hi, are you sure you put the correct url here?

<p><a href="/news/<?= esc($news_item['slug'], 'url'); ?>

As /news/ are not something that get's automatically get set.

Just to make sure, you want https://www.soaringtools.org/ci-news/public to become https://www.soaringtools.org/ci-news ?

If so, this needs to be set in .env
app.baseURL = https://www.soaringtools.org/ci-news/

And for removing "public" you can follow these instructions:

To summarize; Rename "public" into "ci-news".

All other files and folders need to be moved here:

PHP Code:
$pathsPath FCPATH '../../ci-news/app/Config/Paths.php'

Big Grin 
OK, many thanks!  Really appreciate the quick response!
I got it working with the following changes as you suggested:

Moved everything up one directory from public into /pub/comwww/soaringtools/ci-news
[[email protected] ci-news]$ pwd

[[email protected] ci-news]$ dir
app/     composer.json  favicon.ico  license.txt  phpunit.xml.dist  README.md   spark*  vendor/
builds*  composer.lock  index.php    notes.txt    public.old/       robots.txt  tests/  writable/

[[email protected] ci-news]$ git status

#       renamed:    public/.htaccess -> .htaccess
#       renamed:    public/favicon.ico -> favicon.ico
#       renamed:    public/index.php -> index.php
#       renamed:    public/robots.txt -> robots.txt

You were correct, I had missed part of the path needed in the overview.php, but I also had to change it to './news/'

[[email protected] ci-news]$ git diff
diff --git a/app/Views/news/overview.php b/app/Views/news/overview.php
index 73be033..6ca74fe 100644
--- a/app/Views/news/overview.php
+++ b/app/Views/news/overview.php
-        <p><a href="<?= esc($news_item['slug'], 'url'); ?>">View article</a></p>
+        <p><a href="./news/<?= esc($news_item['slug'], 'url'); ?>">View article</a></p>

Also made the change to the index.php, slightly different path, matches dir contents:
--- a/index.php
+++ b/index.php
-$pathsPath = realpath(FCPATH . '../app/Config/Paths.php');
+$pathsPath = realpath(FCPATH . './app/Config/Paths.php');

All now works well!:

https://www.soaringtools.org/ci-news/news   -- and the links to the messages work great!

Hi, for a security point of you. It's wrong I'm afraid. Your index.php should not be together with all other files.

I can now access your notes:

Those folders need to be moved two directories up, please take a second look at my description.

OK, well that was a surprise!  I did move all the other folders/files into the higher directory, outside of the webserver doc root.  I adjusted the path in the index.php and all is well!

I would like to know how you detected the notes.txt file -- there was no directory indexing active?

Also, one other change I 'think' was needed involved the create news item view:

The original 'action' according to the tutorial was:
<form action="/news/create" method="post">

I had to change it to the below:
<form action="<?=site_url()?>/news/create" method="post">

Again, many thanks for the help!

Based on your notes in your previous post, you only moved public/index.php to index.php. You did not move anything else.



But all other folders and files are still here:

That's why I suggested to move those folder two steps down as well. So you can't access them with any url. It looks like you have done so know, at least I can access them any more.

You copied your "dir" command, and listed notes.txt as a file.

You should change that into <?= site_url('news/create'); ?>, makes it look nicer at least. :-)

OK, thanks for all the help.  I wanted to summarize below to make it easier for folks trying to solve the same issue:

GOAL:  Install Codeigniter 4.x using Composer on an existing website where you'd like the new Codeigniter application accessible via a URL referencing a sub directory, e.g.

Existing website: www.yoursite.org,  Apache DocumentRoot: /pub/users/you/yoursite

Desire to have new Codeigniter app accessible as: www.yoursite.org/ci-news

Follow the CodeIgniter 4.x install instructions for the demo app, briefly:

# go to your DocumentRoot
$ cd /pub/users/you/yoursite

# Install codeigniter, app framework.
$ composer create-project codeigniter4/appstarter ci-news --no-dev

# You'll then have a fully populated directory
$ ls /pub/users/you/yoursite/ci-news

app/     composer.json  env          phpunit.xml.dist  README.md  tests/   writable/
builds*  composer.lock  license.txt  public/           spark*     vendor/

NEXT - move stuff around since we want to reference via subdirectory in URL.  Create a higher level directory outside the Document Root.  It will hold all the application files and the Codeigniter install - inaccessible from Apache!

$ mkdir /pub/users/you/ci-news
$ cd /pub/users/you/yoursite/ci-news

# move everything but public to the new directory
$ mv app composer.json env phpunit.xml.dist README.md tests writable uilds* composer.lock license.txt spark* vendor  /pub/users/you/ci-news

# move everything in public up
$ mv public/* public/.htaccess .

# Once you're done you should have the following contents:

$ ls -al /pub/users/you/yoursite/ci-news
favicon.ico  .htaccess  index.php  robots.txt

$ ls -al /pub/users/you/ci-news
app composer.json  env spark  vendor builds composer.lock license.txt phpunit.xml.dist README.md  tests writable

Almost done!  Now edit a few files/permissions.

# Rename env to .env
$ cd /pub/users/you/ci-news
$ mv env .env

# Uncomment the CI_ENVIRONMENT and app.baseURL and change to:
CI_ENVIRONMENT = development
app.baseURL = 'https://www.yoursite.org/ci-news/'

# Change the ownership of the writable directory to be accessible
# by whatever user is used to execute your webserver, e.g.

$ chown -R apache.apache /pub/users/you/ci-news/writable

# IMPORTANT - change the Paths entry in the default home page

$ cd /pub/users/yoursite/ci-news/

# edit index.php
$pathsPath = FCPATH . '../../ci-news/app/Config/Paths.php';

# to check your path above, while you're in that directory, this should work
# using that path

$ ls -l ../../ci-news/app/Config/Paths.php
-rw-r--r-- 1 yoursite www 2821 May  1 01:01 ../../ci-news/app/Config/Paths.php

# one minor change in the tutorial instructions for the file
# app/Views/news/create.php

The original 'action' according to the tutorial was:
<form action="/news/create" method="post">

Change it to the below:
<form action="<?=site_url('news/create');?>" method="post">

You should then see the Codeigniter welcome page at

and be able to complete the first app tutorial!

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.