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
Related
I use smarty and I want to check if my currenty URL contains some value.
Therefore I use the following code, but that does not work.
{if $smarty.server.HTTP_REFERER|strstr:'domain=transfer&sld='}
The full URL;
https://example.com/cart.php?a=add&domain=transfer&sld=value&tld=.com
What am I missing?
Try REQUEST_URI instead of HTTP_REFERER.
This will do it.
I am new to CodeIgniter and I have some issues with its routing config.
Everything works fine if I access a page as it should be
http://localhost/index.php/class/controller
But if I add some random arguments
http://localhost/index.php/class/controller/randomStuff
it displays the page without the CSS or the JS but I would like to display a 404 error page.
Here is my route.php file
$route['stats'] = 'main/account';
$route['/'] = 'index.php';
$route['/(:any)'] = 'main/disperr'; //Tried this to solve the problem but doesn't work (disperr simply returns show_404())
$route['default_controller'] = 'main';
$route['all'] = 'main';
$route['404_override'] = 'errors/page_missing';
$route['translate_uri_dashes'] = FALSE;
Thanks in advance for your help.
Edit : Thank you for your answers !
After routing the pages correctly I stil had the missing css issue.
As I'm using twig, I sent the base_url() as a parameter to my view and added it to the path to my CSS and JS.
It works now.
you may plese the following url
http://localhost/class/index.php/main
after index.php you need to give your name of the controller(not folder name controller)
You have to follow some rules from documentation page.
Routes you found after CodeIgniter installation have to be at the beginning in order you found those.
$route['default_controller'] = 'main';//Main class need to have index() method for this works
$route['404_override'] = 'errors/page_missing';
$route['translate_uri_dashes'] = FALSE;
You have to take care of precedence because rules will be read in order those are defined in file.
$route['all'] = 'main';
$route['stats'] = 'main/account';
$route['(:any)'] = 'main/disperr'; //Tried this to solve the problem but doesn't work (disperr simply returns show_404())
If you have (:any) wildcard placeholder it has to be at the end of file because you have to check all specific routes not to pass (:any) rule, because that rule is valid for any route (as name suggest). Slash to index route is not needed because slash is default controller and it is already defined. Routing to index.php is not valid and you are suppose to route just to 'controller_name/method_name/param1' or similar pattern. No need for slashes at the beginning, as you see I removed those. Maybe removing slashes from the beginning of made routes will solve your CSS issue, but you need to follow other documentation rules I exposed here. Read full page here.
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()
Having problems with routing in CodeIgniter.
Here's my routes.php
$route['default_controller'] = "site/site/index/$1";
$route['404_override'] = '';
$route['admin'] = 'admin/index';
I'm using modular system and my folder format is:
In side the modules > site > controllers >site.php i have the following that gets the path of each that is entered into the cms, but the problem is when i navigate to localhost/my_site/ it shows the page but with errors, so if i put the following route in: $route['site/(:any)'] = 'site/site/index/$1'; it then works and shows all the pages but obviously I don't want this, i want it to know that the home page is set. Or can this be done via htaccess DirectoryIndex ??
If this doesn't make sense or you cant understand what i mean give me a shout and i'll try explain more details, but that's about it the problem.
I think its because you default controller expects a parameter but you are not passing it one
$route['default_controller'] = "site/site/index/$1";
I think it should be
$route['default_controller'] = "site/site/index";
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.