Welcome Guest, Not a member yet? Register   Sign In
Controller in subdirectory
#1

My folder structure is 
/App/Controllers/Api
I have a file named Internal.php with a class name Internal that extends Controller the class has only one public method named bounce
I expected to be able to access the method at '/api/internal/bounce' but I get a 404 instead at that url.

If I move my controller to the /App/Controllers directory I am able to access the method at '/internal/bounce'

Can someone help point me in the correct direction?

Here is my routes file if it helps

PHP Code:
<?php
namespace Config;

// Create a new instance of our RouteCollection class.
$routes Services::routes();

// Load the system's routing file first, so that the app and ENVIRONMENT
// can override as needed.
if (file_exists(SYSTEMPATH 'Config/Routes.php')) {
    require SYSTEMPATH 'Config/Routes.php';
}

/**
 * --------------------------------------------------------------------
 * Router Setup
 * --------------------------------------------------------------------
 */
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Site');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override('App\Controllers\Site::show_404');
$routes->setAutoRoute(true);

/*
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('/''Site::index');
$routes->get('/privacy-policy''Site::privacy_policy');
$routes->get('/terms-of-use''Site::terms_of_use');
$routes->get('/homes-we-have-bought''Site::homes_we_have_bought');
$routes->get('/faqs''Site::faq');
$routes->get('/why-us''Site::why_us');
$routes->post('/thank-you''Site::thank_you');
$routes->get('/careers''Site::careers');
$routes->get('(?i)(\w+[-?\w+?]+)-([a-z][a-z])-cash-home-buyers''Site::city/$1/$2/1');
$routes->get('(?i)(\w[-\w+]+)-([a-z]+)-cash-home-buyers''Site::city/$1/$2/3');
$routes->get('sell-my-house-fast-(?i)(\w+[-?\w+?]+)-([a-z][a-z])''Site::city/$1/$2/2');
$routes->get('/sitemap.xml''Site::sitemap/xml');
$routes->get('/sitemap''Site::sitemap/html');

/* Redirects for old site */

$routes->addRedirect('/fortworth-cash-home-buyers''/fort-worth-tx-cash-home-buyers');
$routes->addRedirect('/corpus-christi-cash-home-buyers''/corpus-christi-tx-cash-home-buyers');
$routes->addRedirect('/midland-odessa-cash-home-buyers''/midland-odessa-tx-cash-home-buyers');
$routes->addRedirect('/get-cash-offer''/');
$routes->addRedirect('/we-buy-houses-for-cash-in-abilene-texas''/abilene-tx-cash-home-buyers');
$routes->addRedirect('/san-antonio-cash-home-buyers''/san-antonio-tx-cash-home-buyers');

/*
 * --------------------------------------------------------------------
 * Additional Routing
 * --------------------------------------------------------------------
 *
 * There will often be times that you need additional routing and you
 * need it to be able to override any defaults in this file. Environment
 * based routes is one such time. require() additional route files here
 * to make that happen.
 *
 * You will have access to the $routes object within that file without
 * needing to reload it.
 */
if (file_exists(APPPATH 'Config/' ENVIRONMENT '/Routes.php')) {
    require APPPATH 'Config/' ENVIRONMENT '/Routes.php';

Reply
#2

(This post was last modified: 06-19-2021, 04:34 PM by venance.)

You need to specify the namespace and the namespace of the extended class or BaseController.

PHP Code:
//routes
$routes->get('/api/(:any)''Api\Internal::bounce/$1');

//controller inside Api directory--- App/Controlles/Api/Internal.php
//localhost/projectname/public/api/internal/bounce

namespace App\Controllers;
use 
App\Controllers\BaseController;

class 
Internal extends BaseController
{
      public function __construct(){}

      public function bounce(){}

Reply
#3

(This post was last modified: 06-19-2021, 05:08 PM by TaylorHicks. Edit Reason: Figured out my problem )

Thank You for your prompt reply.

I have the Controller namespaced and I am extending CodeIgniter\Controller rather than BaseController because my base controller has a lot of code for rendering my sites template pages that I didn't want to load for an API Call thats going to return json. I will probably make a new class 'ApiBaseController' in the near future to extend for my api controllers.

I was hoping to take advantage of the auto -outing feature so I didn't have to declare the routes for every api call and remember to change them if I change something in the future. This is just the first of many Controllers/Methods that will be implemented in this folder and I was hoping to get it working as it is described here CodeIgniter4 Controllers in Sub-Directories.

Edit: I got it working by changing the namespace from 'App\Controllers' to 'App\Controllers\Api'

PHP Code:
namespace App\Controllers;

use 
CodeIgniter\Controller;

class 
Internal extends Controller {

    public function bounce() {}


Reply
#4

Nice if you have solved the issue.
Reply
#5

namespace App\Controllers\SubDirectoryName;
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB