Codeigniter base_url() function, go to parent folder - php

I've an application working since one year, now my client is asking to make a folder outside the App to place the contracts (PDFs) in.
The point is that the app is fully working and has many calls to the base_url() function, so I cannot change the config file $config['base_url'] variable or my app will stop working.
So basically I need to go to the parent folder of my current app.
Let's say the base url is at www.mysite.com/a/app/ I need to go into the www.mysite.com/b/ folder.
My first idea was:
<?= base_url('../b') ?>
But of course does not work. Any idea?

Try this code:
echo base_url(array('.', '..', 'b'));
OR
echo base_url('./../b');

Try this:
echo base_url() . '../../b/';

work for me
back to one folder;
dirname(base_url());
back to two folder ;
dirname(dirname(base_url()));

Related

webroot entire path in Yii2

I have some trouble with an Yii2 application.
I need to put into a variabile the complete webroot path of my application, in my case www.mysite.it/language/catalogue/. When I call the function to retrieve this information, I get www.mysite.it/application/web where application/web is the root of my project.
I have tried this function:
$home = Yii::$app->request->getAbsoluteUrl();
It is any way to do what I want?
Thanks in advance.
You should use yii\helpers\Url features
http://www.yiiframework.com/doc-2.0/yii-helpers-url.html
http://www.yiiframework.com/doc-2.0/guide-helper-url.html
and eg_ for home Url::home()
$absoluteHomeUrl = Url::home(true);
or for Url:to()
$url = Url::toRoute(['language/catalogue/']);
the use of the UrlHelpers prevent your code from different url result related
to your urlManager config (with or without pretty url)

links not working on web server

i have developed a php application which is running perfect on local server. when i deployed the application on web server the links are not working
1) my site is "abc.myapplication.com" (abc is subdomain)
i defined following variable in config file
define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT']);
ROOT_PATH variable shows /home/punjabfo/public_html/abc (which is perfect)
for link i used following code
Add Record
link should go to "abc.myapplication.com/addrecord.php" but link go to
"abc.myapplication.com/home/punjabfo/public_html/abcaddrecord.php"
i tried a lot but could not fin the issue. please help. thanks
What is wrong with Keeping It Simple
Add Record
Let the server do all the work, as it gets it right and you do less messing around.
Try
define('ROOT_PATH', $_SERVER['HTTP_HOST']);
Just use
Add Record
You can try -
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo 'http://'.parse_url($url, PHP_URL_HOST) . '/';
Your issue is "$_SERVER['DOCUMENT_ROOT']". $_SERVER['DOCUMENT_ROOT'] stands for the root directory on the server (dir-path). What you need, is the URL and not the file-system-path.
Take a look on
<?php
echo "<pre>";
var_dump($_SERVER);
echo "</pre>";
?>
Why not to do
Add Record
Of course you do not need ROOT_PATH in the URL. What you do is returning full path of the file, instead of link. And btw, full path is incorrect itself, as you forgot slash before addrecord.php.

Codeigniter Loading external site in default controller

I'm not able to figure this out on my own so here I am asking for your help.
How do I load a website that I already made as a view in the code igniter default controller?
I put my website under a folder name site, and in the default controller I loaded the view site/index , but then in my site there are problems with the includes and redirects... I don't know why, I guess the way the site usually works with redirecting isn't compatible with code igniter style
edit: I guess I would have to turn off CI engine for this site, but I don't know why, because I would still need codeingiter to manage other parts of my application
"CodeIgniter can be told to load a default controller when a URI is not present, as will be the case when only your site root URL is requested. To specify a default controller, open your application/config/routes.php file and set this variable:
$route['default_controller'] = 'Blog';
Where Blog is the name of the controller class you want used. If you now load your main index.php file without specifying any URI segments you'll see your Hello World message by default."
http://codeigniter.com/user_guide/general/controllers.html
fragment copied from that link , you should put the controllers classname in that config, not the view
I guess it's better to choose one of these options:
Modify the existing site to a CodeIgniter site.
Keep your site separate from the CodeIgniter site, and just link between the two sites.
The way you are trying to do it seems very useless and causing a lot of extra trouble.
You can simply use the redirect function in your controller. If you supply a full URL you can go to any other page. You will, of course, leave your CI app.
redirect('http://www.example.net/page_in_external_site/');
Try using the APPPATH constant when defining the paths for the includes.
I know it's an old question, but you can try using a view template with an iframe, and you can pass the URL to the src property of the iframe. That way you can display your site inside a view, but still can't get access to the vars passed to the view from your site.
In system/Core/Loader.php change the line 141 to look like this:
$this->_ci_view_paths = array(APPPATH . 'views/' => TRUE, FCPATH => TRUE);
and to get the view is simple:
$this->load->view('application/ PATH_TO_VIEW');

Help with moving codeigniter to a new server

I am having a bit of a problem. I am duplicating my website to a test environment on a new server with a new domain.
I have it working fine with the database etc but here is my error.
On the live site if I click suppliers it goes to the page fine If I click it on the test environment it does not work.
These are two links
http://wvtest.co.uk/
http://theweddingvine.com/
The code to create the link is as follows:
<?php
echo anchor('search', 'Suppliers');
?>
The file and the folders are the same. Is there something in codeigniter which can direct the link ?
Modify application/config/config.php and set:
$config['base_url'] = "http://{$_SERVER['HTTP_HOST']}/";
That will make your links work on either server.
have you specified the correct base_path for your testing server in application/config/config.php file?
Check that please, this is the case mostly in such problems.
You could change your base_url to
$config['base_url'] = 'http://wvtest.co.uk/';
or
$config['base_url'] = '/';

codeigniter, global variable for beta project path, and access from everywhere

I use CodeIgniter, I'm happy with that, but I have a question.
I build my projects under /www/projectname/beta/... directory, so at my code, at many parts like including some images or css files or etc. I have to make ... src="/projectname/beta/... so when I complete the website, I need to edit so many pages to clear these /projectname/beta/ path and make it / for main root. or when I start new project with same base, first of all I need to edit these paths at all files.
now, how can I define a variable like
$projectbetapath =
"/projectname/beta/";
and have access from everywhere, like global. where can I add such line, and how can I access this var from everywhere?
Thanks!! appreciate!
Why don't you add a constant in your index.php file?
define('BETA_PATH', '/beta');
When the site leaves the beta stage you just do:
define('BETA_PATH', '');
There are two answers to your question:
Set your variables as array fields of $config in application/config/config.php and access them with $this->config->item('name');
Use the URL-helper (Or $this->config->item('base_url')) to get the current base path whenever you have to type in a path.
The second answer will give you full flexibility, you'll only have to modify the base URL in config.php if the project moves.

Categories