Welcome Guest, Not a member yet? Register   Sign In
beta 2 - base_url() inconsistent in namespaces
#1

Greetings.  Is this a bug or am I missing something basic?

base_url() is correct when referenced in the app namespace, but is incorrect in other namespaces (i.e. modules).  
site_url() is correct in both namespaces.

Here is a minimal example of the code I used to create, use, and route modules and the "quirk".

PHP Code:
SETUP:
App/Config/App.php:
       public 
$baseURL 'http://localhost/ci4/';  
       
// the paths.php file has all the directories changed to the real directories on the C: drive
       // The public/index.php was modified to point to the actual config/paths.php file 
       // and index.php was then copied to the localhost directory.  That is the only file on localhost/ci4.

App/Config/Autoload.php:
$psr4 = [
'    Config'       => APPPATH 'Config',
    
APP_NAMESPACE => APPPATH               // For custom namespace
    
'Auth'          => ROOTPATH 'Auth',      // For the module
    
'App'         => APPPATH               // To ensure filters, etc still found,
];

App/Config/Routes.php:
$routes->add'login' 'Auth\Controllers\Login::index');
$routes->add'logout''Auth\Controllers\Login::logout');
$routes->add'/'     'App\Controllers\Home::index');


APPLICATION:

App/Controllers/Home.php:
// site_url() and base_url() both = "http://localhost/ci4/" here
// Make sure user is logged in first: 
$sec = new \Auth\Models\Secmgmt(); 
$security $sec->sec_check();
// if not...
if (!$security['logged_in']) $this->response->redirect(base_url('login'));


Auth/Controllers/Login.php:
// Login form is processed and all is good.  Return to Home now.
// if ($data['logged_in'])  $this->response->redirect(base_url("/"));  ===> goes to 'http://localhost/' and not routed
   
if ($data['logged_in'])  $this->response->redirect(site_url("/"));  ===> goes to 'http://localhost/ci4/'  Yay

Any ideas why the redirect to base_url() fails here?  Same result as $this->response->redirect('/').

Also, it seems that the response class is not available from any models code.  Is that by design, can I redirect in the model code, or did I goof up there too?


Cheers.
Reply
#2

base_url() should be the same from any namespace. So that definitely sounds like a bug. PLease file a bug report for that!

The request and response classes are only automatically inserted as a property in controllers, so $this->response is only available in controller, not model.

In my opinion you shouldn't be messing with response or requests from within a model, but if you want to, you can always grab the current instance through the services:

Code:
$request = \Config\Services::request();
$response = service('response')

There's two ways to grab a service so I showed both for example, but either way would work with any service.
Reply
#3

Thanks!    I agree that navigation is controller only.  I was just attempting shortcuts in development/test.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB