Welcome Guest, Not a member yet? Register   Sign In
Please Help with routing
#1

Hello all,  I'm starting with CI4 (used CI3 for some projects) but this is my first time using routes and I want to know what do you recommend for this:

In my new project I want to have a website, its almost only static pages with some database calls (cities and product categories) and also will have a user area where they will be able to manage products.

According to docs should be like this:

/app       <-- Everything else in the app
/website  <-- All related to website
/public
/system
/tests
/writable


Website part should have only one controller and a view for any page (only 4 pages) and should work in root like this:
domain.com   (home page)
domain.com/services
domain.com/products
domain.com/contact

App part should have everything else and should work with 'app' (or whatever) prefix like:
domain.com/app/login
domain.com/app/users
domain.com/app/products
domain.com/app/cities
.
.

Is this ok?  Can anyone help me please with a routing example for achieving this?

Also should I use .env file for app settings or is it a better practice to create a dedicated config file for my app?
Reply
#2

If you look at the Tutorial in the CodeIgniter 4 User Guide it will explain it all to you.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 11-04-2020, 02:06 AM by captain-sensible.)

reading the docs will explain everything thats true, but a little guidance to get you in the right direction .We all need a little help :^)


At some point there will be updates and probably updates for additional software you don't know of yet and don't think you need when you do its going to be easier with composer. So i would say give https://getcomposer.org/ a good read first.

There are options for CI4 , my preference is for appstarter which you can install using composer.

I have a core appstarter called basic to play with some concepts i'm learning. When you use a command terminal and cd say to Desktop and use this command:

Code:
composer  create-project codeigniter4/appstarter --no-dev
you should get a directory called appstarter. For you its going to be something like

Code:
php composer.phar create-project codeigniter4/appstarter --no-dev
//you will have to play with concepts of using composer locally and globally . I renamed mine to "basic" lets have a look:




Code:
basic
├── README.md
├── app
│   ├── Common.php
│   ├── Config
│   ├── Controllers
│   ├── Database
│   ├── Entities
│   ├── Filters
│   ├── Helpers
│   ├── Language
│   ├── Libraries
│   ├── Models
│   ├── ThirdParty
│   ├── Views
│   └── index.html
├── builds
├── composer.json
├── composer.lock
├── env
├── license.txt
├── phpunit.xml.dist
├── public
│   ├── captcha
│   ├── css
│   ├── errors
│   ├── favicon.ico
│   ├── fonts
│   ├── images
│   ├── index.php
│   ├── js
│   ├── minCss
│   ├── portfolio
│   ├── robots.txt
│   └── sounds
├── spark
├── tests
│   ├── README.md
│   ├── _support
│   ├── database
│   ├── session
│   └── unit
├── vendor
│   ├── autoload.php
│   ├── bin
│   ├── codeigniter4
│   ├── composer
│   ├── doctrine
│   ├── fzaninotto
│   ├── kint-php
│   ├── laminas
│   ├── mikey179
│   ├── myclabs
│   ├── phar-io
│   ├── phpdocumentor
│   ├── phpspec
│   ├── phpunit
│   ├── psr
│   ├── sebastian
│   ├── symfony
│   ├── theseer
│   └── webmozart
└── writable
    ├── Art2
    ├── Art22
    ├── cache
    ├── debugbar
    ├── logs
    ├── session
    └── uploads

your controllers go in contollers, views go in views and then for css, images such as logo that won't change , go to public and make directories called " images", "css" "js", "sounds", . Then put your assetts such as your css in css directory..sound clips in sounds.. yep you got the idea.
I'm using sqlite database and i put those 2 in writable, because yep its writable and a database needs to be written too .

Last bit to display appropriate page for domain.com/cities you create a route in Config/Routes.php

lets say:
$routes->get('/', 'Home::index');
//above is home route it uses Controller Hone and method index

$routes->get('cities', 'City::index');
"cities" tells system that when it gets a get request with domain.com/cities

it should use a controller called City.php and its method index()
{


}

inside the method index you tell system what to do . read docs on getting started.
CMS CI4     I use Arch Linux by the way 




Arch Book
Reply
#4

Thank you so much for your guide captain-sensible, already had my project installed even with layout but Im going to do everything again to give composer a try
Reply
#5

Ok just to update if anyone is interested, i decided I was overcomplicating this with the prefix I wanted to separate app from website now Im going with a structure like this:

/app
--/controllers
----/app/             <-- Everything else in the app
----website.php   <-- All related to website

/public
/system
/tests
/writable
Reply




Theme © iAndrew 2016 - Forum software by © MyBB