CodeIgniter Forums

Full Version: Sub Directory on Controller
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello,

I decided to try codeigniter version 4 now,
.
But I found a problem when trying to use the subdirectory in my controller.
.
This preview my code.
.


[Structor Subdirectory + Class]
[Image: YMbwJhm.jpg]

[Controller Shoes.php]
[Image: cf4nLPv.jpg]

[Accessing Url]
[Image: 3voDAfg.png]

^_^ please help me to make this work .. thanks brother
Fix.. use namespace using sub-directory itself like this...
.
Thanks ^_^

[Code Preview]
[Image: u9ShYBa.png]
Thanks for figuring this out. I was having the same problem and this solved it.
I have currently the same problem but for whatever reason i won't work Sad

Any idea?


[Image: ci4.png]

PHP Code:
<?php

namespace App\Controllers\products;

use 
CodeIgniter\Controller;

class 
Shoes extends Controller
{
 
   public function index()
 
   {
 
   }
 
   
    public 
function test()
 
   {
 
       echo "test";
 
   }

(05-02-2019, 01:44 AM)michael.j Wrote: [ -> ]I have currently the same problem but for whatever reason i won't work Sad

Any idea?


[Image: ci4.png]

PHP Code:
<?php

namespace App\Controllers\products;

use 
CodeIgniter\Controller;

class 
Shoes extends Controller
{
 
   public function index()
 
   {
 
   }
 
   
    public 
function test()
 
   {
 
       echo "test";
 
   }


What doesn't work? how do you access your page (ex: http: //localhost/ci4/index.php/yourcontroller/yourfunction)? ^_^ please also screenshot your browser view Blush
I access the page via ip because the server is running as a virtual machine and DNS is currently not set up finally. So the current URL is always 192.168.10.40

Working: http://192.168.10.40/home
Working: http://192.168.10.40/index.php/home
Working: http://192.168.10.40/index.php/home/test

Not working; http://192.168.10.40/products
Not working; http://192.168.10.40/products/shoes
Not working; http://192.168.10.40/products/shoes/test

Browser Screenshot
[attachment=1522]

php -m

Code:
[PHP Modules]
calendar
Core
ctype
date
dom
exif
fileinfo
filter
ftp
gettext
hash
iconv
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
Phar
posix
readline
Reflection
session
shmop
SimpleXML
sockets
sodium
SPL
standard
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xml
xmlreader
xmlwriter
xsl
Zend OPcache
zip
zlib

[Zend Modules]
Zend OPcache


apachectl -M

Code:
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 192.168.10.40. Set the 'ServerName' directive globally to suppress this message
Loaded Modules:
 core_module (static)
 so_module (static)
 watchdog_module (static)
 http_module (static)
 log_config_module (static)
 logio_module (static)
 version_module (static)
 unixd_module (static)
 access_compat_module (shared)
 alias_module (shared)
 auth_basic_module (shared)
 authn_core_module (shared)
 authn_file_module (shared)
 authz_core_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 deflate_module (shared)
 dir_module (shared)
 env_module (shared)
 filter_module (shared)
 mime_module (shared)
 mpm_prefork_module (shared)
 negotiation_module (shared)
 php7_module (shared)
 proxy_module (shared)
 proxy_fcgi_module (shared)
 reqtimeout_module (shared)
 rewrite_module (shared)
 setenvif_module (shared)
 status_module (shared)


Apache Error Log
Code:
[Fri May 03 09:54:17.396759 2019] [authz_core:debug] [pid 20102] mod_authz_core.c(809): [client 192.168.168.68:4670] AH01626: authorization result of Require all granted: granted
[Fri May 03 09:54:17.396772 2019] [authz_core:debug] [pid 20102] mod_authz_core.c(809): [client 192.168.168.68:4670] AH01626: authorization result of <RequireAny>: granted

Apache Access Log /products
Code:
"GET /products HTTP/1.1" 404 1509 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"


Apache Access Log /home
Code:
"GET /home HTTP/1.1" 200 1899 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"

.htaccess inside /public directory
Code:
# ----------------------------------------------------------------------
# Environment Name
# ----------------------------------------------------------------------

# Sets the environment that CodeIgniter runs under.
# SetEnv CI_ENVIRONMENT development

# Disable directory browsing
Options All -Indexes

# ----------------------------------------------------------------------
# UTF-8 encoding
# ----------------------------------------------------------------------

# Use UTF-8 encoding for anything served text/plain or text/html
AddDefaultCharset utf-8

# Force UTF-8 for a number of file formats
<IfModule mod_mime.c>
   AddCharset utf-8 .atom .css .js .json .rss .vtt .xml
</IfModule>

# ----------------------------------------------------------------------
# Activate CORS
# ----------------------------------------------------------------------

<IfModule mod_headers.c>
   <FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js|gif|png|jpe?g|svg|svgz|ico|webp)$">
       Header set Access-Control-Allow-Origin "*"
   </FilesMatch>
</IfModule>

# ----------------------------------------------------------------------
# Rewrite engine
# ----------------------------------------------------------------------

# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On

  # If you installed CodeIgniter in a subfolder, you will need to
  # change the following line to match the subfolder you need.
  # http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
  RewriteBase /

  # Redirect Trailing Slashes...
   RewriteRule ^(.*)/$ /$1 [L,R=301]

  # Rewrite "www.example.com -> example.com"
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

  # Checks to see if the user is attempting to access a valid file,
   # such as an image or css document, if this isn't true it sends the
   # request to the front controller, index.php
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php/$1 [L]

   # Disable image hotlinkiing start
   RewriteCond %{HTTP_REFERER} !^$
   RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example.com [NC]
   RewriteRule \.(jpg|jpeg|png|gif)$ – [NC,F,L]
   # Disable image hotlinkiing end

  # Ensure Authorization header is passed along
   RewriteCond %{HTTP:Authorization} .
   RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

<IfModule !mod_rewrite.c>
   # If we don't have mod_rewrite installed, all 404's
   # can be sent to index.php, and everything works as normal.
   ErrorDocument 404 index.php
</IfModule>

# Disable server signature start
   ServerSignature Off
# Disable server signature end

# BEGIN Expires
<ifModule mod_expires.c>
  ExpiresActive On
   ExpiresByType text/css "access 1 month"
   ExpiresByType text/html "access 1 month"
   ExpiresByType image/gif "access 1 year"
   ExpiresByType image/png "access 1 year"
   ExpiresByType image/jpg "access 1 year"
   ExpiresByType image/jpeg "access 1 year"
   ExpiresByType image/x-icon "access 1 year"
   ExpiresByType image/svg+xml "access plus 1 month"
   ExpiresByType audio/ogg "access plus 1 year"
   ExpiresByType video/mp4 "access plus 1 year"
   ExpiresByType video/ogg "access plus 1 year"
   ExpiresByType video/webm "access plus 1 year"
   ExpiresByType application/atom+xml "access plus 1 hour"
   ExpiresByType application/rss+xml "access plus 1 hour"
   ExpiresByType application/pdf "access 1 month"
   ExpiresByType application/javascript "access 1 month"
   ExpiresByType text/x-javascript "access 1 month"
   ExpiresByType text/x-component "access plus 1 month"
   ExpiresByType application/x-shockwave-flash "access 1 month"
   ExpiresByType font/opentype "access plus 1 month"
   ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
   ExpiresByType application/x-font-ttf "access plus 1 month"
   ExpiresByType application/font-woff "access plus 1 month"
   ExpiresByType application/font-woff2 "access plus 1 month"
   ExpiresDefault "access 1 month"
</ifModule>
# END Expires

# ----------------------------------------------------------------------
# Gzip compression
# ----------------------------------------------------------------------

# Start gzip compression
<IfModule mod_gzip.c>
   mod_gzip_on Yes
   mod_gzip_dechunk Yes
   mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
   mod_gzip_item_include handler ^cgi-script$
   mod_gzip_item_include mime ^text/.*
   mod_gzip_item_include mime ^application/x-javascript.*
   mod_gzip_item_exclude mime ^image/.*
   mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>
# End gzip compression

<IfModule mod_deflate.c>

  # Force deflate for mangled headers developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping/
  <IfModule mod_setenvif.c>
     <IfModule mod_headers.c>
        SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
        RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
     </IfModule>
  </IfModule>

  # Compress all output labeled with one of the following MIME-types
  # (for Apache versions below 2.3.7, you don't need to enable `mod_filter`
  # and can remove the `<IfModule mod_filter.c>` and `</IfModule>` lines as
  # `AddOutputFilterByType` is still in the core directives)
  <IfModule mod_filter.c>
     AddOutputFilterByType DEFLATE application/atom+xml \
                                   application/javascript \
                                   application/json \
                                   application/rss+xml \
                                   application/vnd.ms-fontobject \
                                   application/x-font-ttf \
                                   application/xhtml+xml \
                                   application/xml \
                                   application/x-javascript \
                                     application/x-font \
                                     application/x-font-truetype \
                                     application/x-font-otf \
                                     application/x-font-woff \
                                     application/x-font-woff2 \
                                     application/x-font-opentype \
                                   font/opentype \
                                   font/ttf \
                                     font/otf \
                                     font/eot \
                                     font/woff \
                                     font/woff2 \
                                   image/svg+xml svg svgz \
                                   image/x-icon \
                                   text/css \
                                   text/html \
                                   text/plain \
                                   text/x-component \
                                   text/xml \
                                   text/javascript \

         # For Olders Browsers Which Can't Handle Compression
           BrowserMatch ^Mozilla/4 gzip-only-text/html
           BrowserMatch ^Mozilla/4\.0[678] no-gzip
           BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
   </IfModule>
</IfModule>
Any sub folders you create need to be name spaced.
I see there is inconsistency in writing the url.. please try to access via url like this...

http://192.168.10.40/index.php/products
http://192.168.10.40/index.php/products/shoes
http://192.168.10.40/index.php/products/shoes/test

I hope it works
Products = Controller

shoes and shoes/test both = Methods

Controller = segment 1
shoes = segment 2
test = segment 3

CodeIgniter 4 User Guide - Passing URI Segments to your methods
Hi everyone,

I am facing the same problem but with no luck.

According to CI4 documentation found here: https://codeigniter4.github.io/userguide...irectories

I should simply create subfolder with my controller in it.

This is my controller:

Code:
<?php
namespace App\Controllers\dashboard;

use App\Controllers\BaseController;

class Dashboard extends BaseController
{
    public function index() {
        $data['page_title'] = 'Dashboard';
        echo view('dash', $data);
    }
}

access url is: myenv.dev.local/dashboard/dashboard/index

PS I have an apache virtualhost pointing to public/ folder as said in the documentation.

Am I missing something?

Thanks to all in advance!
Pages: 1 2