how to get CI config set variable value? - codeigniter

how to get CI config set variable value?
I am working in CI. I have require base url in Controller file.
I am use base_url(); function but show error.

You can use constants for base url for eg.define('BASE_URL','http://domain.com') in constant.php

have you load the helper of url??
Go to (your project)->Source Files->application->config->autoload.php
then find
$autoload['helper'] = array();
and load url like this:
$autoload['helper'] = array('url');

Related

I'm having troubles reading an excel file in laravel

I'm trying to read an excel file(xlsx), I defined the path using
$uri = "public/storage/resultsheet/Revival Royal Academy_Primary 5B_1572753672.xlsx";
but I keep getting the error
File "public/storage/resultsheet/Revival Royal Academy_Primary 5B_1572753672.xlsx"
does not exist. I'm new to this framework.
This is the controller code.
public function show($id)
{
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
$reader->setReadDataOnly(TRUE);
$uri = "public/storage/resultsheet/Revival Royal Academy_Primary 5B_1572753672.xlsx";
$spreadsheet = $reader->load($uri);
$worksheet = $spreadsheet->getActiveSheet();
return view('student.result', compact('worksheet'));
}
The path you're using is relative to the current directory that PHP is running in. For Laravel, that likely means you'll be running this in app/Services/MyUploadService.php or something similar.
Given your path is relative, PhpOffice will probably be trying to load `app/Services/public/storage... etc.' which is incorrect.
Laravel has helper methods that can assist you in working with paths within your app.
You could look at using base_dir() (so: base_dir('public/storage/example.xlsx').) as it will give you an absolute path, and a similar usage of public_path() would be better than that.
However, best out of all the options would be using storage_path(). So your code would instead read:
$reader->setReadDataOnly(TRUE);
$uri = storage_path("resultsheet/my.xlsx");
$spreadsheet = $reader->load($uri);
There's helpful documentation on the Path helpers in the Laravel docs.
To debug this, storing realpath('subdir/example.txt') etc. in a variable and using a debugging tool will help you figure out the relative/absolute paths in your project and server.

facing some issue related to codeigniter url routing

I just want to show the url websitename/common_controller/about as websitename/about
I did like this in the config/routes.php
$route['about'] = "common_controller/about";
But it is not working. please help me.
Try writing
$route['common_controller/about'] = 'about';
if you method takes input you can write
$route['page/(:any)'] = 'method_name/$1';

codeigniter - use custom config value inside CI config file

I got custom.php (inside config folder) with code like below
$config['myurl'] = 'somesite.com';
I got config.php ( standard CI ), I want to set the base_url using the value from custom.php, like this
$config['base_url'] = $this->config->item('myurl');
Doing that I got the error
Using $this when not in object context in /Volumes/HD 2/work/vnl/app/config/config.php on line 18
Whats the right code for this purpose?
This is not going to work there. The $this that you would want to use there is a controller instance and that is not created yet when the standard application/config/config.php loads.
You can try to add a pre_system hook (that's loaded right before the config object) and include in some helper function that can be called and return the desired value in the application/config/config.php. The usual constants like APPPATH will be available then.
If you can make this config variable a constant that can work too (just put the define() inside application/config/constants.php). Since the config file is just a regular php source file, you can have conditionals here too if you must.
You can just work with the arrays.
In your custom config file
$config['myurl'] = 'myurl.com';
$config['base_url'] = &$config['myurl'];
This will change the default base_url once the custom config file is loaded, you can also just reset the base_url element in your custom config, however in my opinion it is not recommended to reset the base url in another config file as later in the project you could lose track of where it is set, why not set it (and maybe use conditions) in the main config file?

get application path codeigniter with out contents after last slash

hi i am using codeigniter in my application .
for my application i need application path only before the last slash
like this
if application path is
http://localhost/elephanti/connections/fb_connection/invite_friends
i want to get
http://localhost/elephanti/connections/fb_connection
it is for a plae like this
echo "<script type='text/javascript'>top.location.href = '"+APPPATH+"'testapp';</script>";
i tried to use '"+APPPATH+"'../testapp' but could not , please help ...................
If you know the last part will always be the 4th, you can use a "dirty way" like this:
$url = site_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$this->uri->segment(3));
And then:
echo "<script type='text/javascript'>top.location.href = '".$url."'/testapp';</script>";
// here, though, I don't understan how "testapp" goes into the url..as a segment?
This will create a valid CI url using only the first 3 segments.
Alternatively, you can use the uri_string() function, wich returns only the segment part of the url. On this, you can explode/str_replace/array_pop/do whatever you need to and pass the new elaborated string on the site_url() function wichi will build the correct URL for you.
I don't understand what "testapp" is there or, do you want to substitute the last segment of your url with that? So that you have, in your example, http://localhost/elephanti/connections/fb_connection/testapp ?
Keep in mind that both uri_string() and site_url() are functions from the URL helper so you need to load it.
In Codeigniter APPPATH referes to the location on the disk and not the url of the application. What you are looking for is base_url(). base_url() return the base location of your site, which usually is the domain.
Concatenation in php is done with the dot operator: "."
echo "top.location.href = '".base_url()."'testapp';";

how to run a function in a config file on codeigniter

How to run an function in config file ?
when i try this, i getting an error like this
Fatal error: Call to undefined function setting() in C:\wamp\www\urunsite\application\config\site.php on line 7
my config file
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// default language
$config['lang'] = 'tr';
// Default user role id
$config['default_role'] = setting('company', 'name');
/* End of file site.php */
the setting() function is auto loading.
please help me.
Config files are loaded early in execution, but you should have no problem running any functions there as long as they have been defined. If you need access to functions that aren't defined at config load time, you have no choice but to load the config file manually instead of automatically, or use a hook to load the needed resources. You will have to use the latter if you want to run helper functions in a core config file, custom config files are usually loaded on demand so it's a bit easier.
setting() isn't a valid function.. If it's your own custom function from a model file or elsewhere, you're not giving this config file proper access to that function.
You said that the setting() function is autoloading, but how is it being autoloaded? Is it from a custom Library? Helper? Model? Depending on which it is, you would need to call the function with:
$this->library_name->setting('company', 'name');
or
$this->model_name->setting('company', 'name');
etc.

Resources