Welcome Guest, Not a member yet? Register   Sign In
404 default Controller Home 2nd method
#1

Hi,
i setup a new Project with CI4. 
app/Config/Feature.php
PHP Code:
public bool $autoRoutesImproved true

app/Config/Routing.php
PHP Code:
public string $defaultController 'Home';
public 
bool $autoRoute true

app/Config/Routes.php
PHP Code:
<?php

use CodeIgniter\Router\RouteCollection;

/**
 * @var RouteCollection $routes
 */
//$routes->get('/', 'Home::index');

service('auth')->routes($routes); 

app/Controllers/Home.php
PHP Code:
<?php

namespace App\Controllers;

class 
Home extends BaseController
{
    public function getIndex(): string
    
{
        return "Home";
    }
    public function getTest()
    {
        
        
return "Test";
    }


But i get a 404 on Https://xxxxx.de/Test
with following error 
Quote:Cannot access the default controller "\App\Controllers\Home::getTest"

What am i missing?
I don't wanna write every route in app/Config/Routes.php

Best regards.
Reply
#2

The basic URL pattern is "https://example.com/class/method/arg"
See https://codeigniter.com/user_guide/incom...i-segments

If you want to have "https://xxxxx.de/test", create Test::getIndex().
Reply
#3

Thank you for your reply kenjis,

I read the Docs, not 100% but 70-80% and i have some experience with some CI4 Projects, but never use the Default Controller with more methods.

The following urls didn't work, but i dont know why not.

Https://xxxxx.de/Home
Quote:Cannot access the default controller "\App\Controllers\Home" with the controller name URI path.
Https://xxxxx.de/Home/Test
Quote:Cannot access the default controller "\App\Controllers\Home" with the controller name URI path.

What am i missing or is it not possibly to have more than Index method in the default Controller?

Best regards
Reply
#4

(06-21-2024, 02:01 AM)TakonSix Wrote: is it not possibly to have more than Index method in the default Controller?

No, it is not possible.

Quote:Important
You cannot access the default controller with the URI of the controller name. When the default controller is Home, you can access example.com/, but if you access example.com/home, it will be not found.
https://codeigniter.com/user_guide/incom...on-options
Reply
#5

(06-21-2024, 02:01 AM)TakonSix Wrote: The following urls didn't work, but i dont know why not.

Https://xxxxx.de/Home
Quote:Cannot access the default controller "\App\Controllers\Home" with the controller name URI path.

That is for security reasons.
Auto Routing Improved permits only one URI for a controller method.

In this case,
Home::getIndex() -> https://xxxxx.de/ (not https://xxxxx.de/home)
Reply
#6

(06-21-2024, 02:27 AM)kenjis Wrote:
(06-21-2024, 02:01 AM)TakonSix Wrote: is it not possibly to have more than Index method in the default Controller?

No, it is not possible.

Quote:Important
You cannot access the default controller with the URI of the controller name. When the default controller is Home, you can access example.com/, but if you access example.com/home, it will be not found.
https://codeigniter.com/user_guide/incom...on-options

I am sorry that i read over this O.o
Thank you for taking the time to explain this to me.
Reply
#7

It is true that auto routing improved is a bit complex and difficult to understand.

If you use it now, I recommend you enable $translateUriToCamelCase.
See https://codeigniter.com/user_guide/incom...-camelcase

Also, spark routes command may help you to understand.
See https://codeigniter.com/user_guide/incom...ark-routes
Reply




Theme © iAndrew 2016 - Forum software by © MyBB