Use CodeIgniter's base url inside a different config file - codeigniter

I'm trying to get another framework working inside of CodeIgniter and this other framework has it's own config file. One of the values it needs is the site or base url. I thought I'd simply use CI's base url value from the application/config/config.php file. I've tried two methods:
Simply use base_url(). The url helper is being autoloaded. This produces "Fatal error: Call to undefined function base_url()"
Do a require_once APPPATH . 'config/config.php';, then use $config['base_url']. This method works for using application/config/database.php's database values. But for the base url, I get this error: "Message: Undefined variable: config". I don't understand why it works for database.php but not config.php.
What is the correct way to do this?

not sure if this will help but try it - go to your config file and make base url blank
$config['base_url'] = '';
go to the main index.php and look for
* -------------------------------------------------------------------
* CUSTOM CONFIG VALUES
then insert this with your url
$assign_to_config['base_url'] = 'http://yourdomain.com';
you should then be able to use base_url()

Related

Using site_url() in Codeigniter without getting a ? in the generated URL

I am trying to use site_url() in Codeigniter, but every time I put something in the parameters (for example site_url('controller_name'); I get a url in my link that looks something like this:
http://{mysite}/index.php?controller_name
Instead of what I think I should be getting which is:
http://{mysite}/index.php/controller_name.
I am using the echo site_url() syntax in a link on my webpage but so far I have been unsuccessful.
Any suggestions?
i think its problem with your config file
check your config file is this
$config['uri_protocol'] = 'QUERY_STRING';
then change in to this
$config['uri_protocol'] = 'AUTO';
instead of using site_url() use
base_url();
it can use
echo base_url().'controller_name';
it gives
http://{mysite}/index.php/controller_name format

Codeigniter - retrieve parameter without controller name in URL

I'm trying to retrieve string as parameter using following URL scheme:
www.myapp.com/[String]
Is there any way I can do this?
*Based on my research, Codeigniter doesn't accept string parameters unless I include the Controller's name in URL: www.myapp.com/[Controller Name]/[String]
But this doesn't solve my problem :(
You're right, CI requires controller name at URI, but You can use default_controller.
At config/routes.php add route rule $routes['(:any)'] = 'welcome/index';, remove index.php from Your URL (there're many tutorials and how-to for this), and at last useuriclass at Yourindex()method ofwelcome` controller:
function index(){
var_dump($this->uri->uri_string());
}

Codeigniter Routes Conflict

I am using codeigniter to create a restapi and I am having some problems with routes.
Here is the equation:
I have to navigate /users/ to index function,
I have to navigate /users/{MongoId} to /users/show/{MongoId},
I have to navigate /users/function to /users/function.
And here, my routes:
$route['api/users/do_login'] = "api/users/do_login";
$route['api/users/(.*)'] = "api/users/show/$1";
When I remove the (.*) routing (or both of them), my do_login function works successfully. But not my api-index function because Codeigniter takes MongoId as function name and fails.
When I write it back (or both), my index function works successfully, but my login doesn't.
Because it tries to send function name to show function as parameter.
Can you please help me to fix that?
Reverse the order of routes, CodeIgniters routes are prioritized.
You seek this structure:
$route['api/users/(:any)'] = "api/users/show/$1";
$route['api/users/do_login'] = "api/users/do_login";
also use (:any) instead of (.*) they are the same.
CodeIgniter Routing
Here, the working routes.
$route['api/users/do_login/(:any)'] = "api/users/do_login/$1";
$route['api/users/(:any)'] = "api/users/show/$1";

Remove controller name from codeigniter 2 url path

I am having trouble removing the controller name from my url path on my localhost.
i have this url - localhost:8888/localhost/site_name/
i have been able to remove index.php from the url using my htaccess similar to http://codeigniter.com/wiki/mod_rewrite so that:
localhost:8888/localhost/site_name/index.php/controller_name
is now:
localhost:8888/localhost/site_name/controller_name/
but i can't remove the controller name from the path so that:
localhost:8888/localhost/site_name/controller_name/function_name/
becomes:
localhost:8888/localhost/site_name/function_name/
I am using only one controller, and i have added:
$route['^(function_name1|function_name2|function_name3)(/:any)?$'] = 'controller_name/$0';
$route['^(?!ezstore|ezsell|login).*'] = "home/$0"; /*similar variation i tried*/
and other variations to my routes file but it does not have any effect. i also tried using the _remap function but that does not help in this case.
Any help will be appreciated! Thanks
You can use a wildcard route,
$route['(:any)'] = "controller_name/$1";
Then when if you go to http://localhost/function_one/param1
it will call the controller controller_name the function function_once and pass param1 as the first parameter.
nb: I must point out, using only one controller for an entire site does raise warning bells for me, you may want to get your code design checked out, but that's just me.

How to convert _ to - in urls for codeigniter?

Been trying to learn codeigniter, 1 problem I had though is that if I had a function say
top_10()
It would mean that my urls will be something like
..../top_10/
Which is fine but I prefer - more than _ for urls. I tried changing my function names to top-10(), but it results in a syntax error it seems (even if it doesn't it results in a ugly function name), Is there a way to let codeigniter auto converts all the _ in my controller functions to - when it comes to the urls?
.../top_10/ -> .../top-10/ for all other similar controller functions.
You can use the URI routing rule
something like :
$route['controller/top-10'] = "controller/top_10";
Add this line in the routes.php file inside application/config/routes.php
Here controller/top-10 will be the part of your url which will be routed to the desired function provided by on the right hand side.
also if you aren't using mod_rewrite to rewrite your url then your URL will appear some thing like
http://localhost/index.php/controller/top-10
Inside your application/config/routes.php folder if there isn't a line for translating uri dashes then add this line: $route['translate_uri_dashes'] = TRUE;
If $route['translate_uri_dashes'] already exists, simply set it's value to true.

Resources