How to know actual url in codeigniter - codeigniter

I got into this error:
404 Page Not Found
The page you requested was not found.
But I want to know where am I? So I can fix the URL.
If it possible how to print the actual URL out.
Update: What is the controller and function.

to debug such errors, you can do this
in application/config/routes.php at the end of the file add
$route[(:any+)] = 'process/processing/$1/$2/$3/$4';
create a file in application/controller/process.php
function processing($controller, $method, $param1, $param2){
echo "$controller <br> $method <br> $param2 <br> $param2";
}
Hope this helpyou

Related

How to create download link in Laravel

I am having problem creating a download link to download files via a Mobile App from Laravel Storage folder.
I did something like $link = Response::Download(storage_path()./file/example.png) but to no avail.
I moved the file to the public folder and used http://domain.com/file/example.png and asset('file/example.png') but to no avail.
I am getting 404 NOT FOUND ERROR
How do I solve this?
Take a look at the Laravel Helpers documentation: http://laravel.com/docs/4.2/helpers
If you want a link to your asset, you can do it like this:
$download_link = link_to_asset('file/example.png');
Edit
If the above method does not work for you, then you can implement a fairly simple Download route in app/routes.php which looks like this:
Note this example assumes your files are located in app/storage/file/ location
// Download Route
Route::get('download/{filename}', function($filename)
{
// Check if file exists in app/storage/file folder
$file_path = storage_path() .'/file/'. $filename;
if (file_exists($file_path))
{
// Send Download
return Response::download($file_path, $filename, [
'Content-Length: '. filesize($file_path)
]);
}
else
{
// Error
exit('Requested file does not exist on our server!');
}
})
->where('filename', '[A-Za-z0-9\-\_\.]+');
Usage: http://your-domain.com/download/example.png
This will look for a file in: app/storage/file/example.png (if it exists, send the file to browser/client, else it will show error message).
P.S. '[A-Za-z0-9\-\_\.]+ this regular expression ensures user can only request files with name containing A-Z or a-z (letters), 0-9 (numbers), - or _ or . (symbols). Everything else is discarded/ignored. This is a safety / security measure....
Updating answer for Laravel 5.0 and above:
<a href={{ asset('file/thing.png') }}>Thing</a>
You do not need any route or controller.Just give it to anchor tag.
<a href="{{URL::to('/')}}/file/example.png" target="_blank">
<button class="btn"><i class="fa fa-download"></i> Download File</button>
</a>

what does it mean disallowed character in URI?

i have written this in my view page,
<a onclick="window.open('print_mine/id=<?php echo $value['id'];?>','800','400')">View</a>
it is giving me The URI you submitted has disallowed characters.
i need id in other page
i have written code in route.php as well as in controller
Look into your config/config.php and search for "disallowed" or "illegal". There you have some chars that are disallowed in CIs URIs.
Maybe you want to window.open('print_mine/<?=$value['id']?>','800','400')?
Your view:
<a onclick="window.open('print_mine/<?php echo $value['id'];?>','800','400')">View</a>
In controller:
function print_mine(){
$id = $this->uri->segment(3); #will fetch the third segment
}

Change Codeigniter url

I'm using Codeigniter.I want to set href attr to something like :
<a href="/contact.html" >Contact</a>
But i get 404 error because i should write
Contact.
Where is some thing to fix this problem.
Any help please.
Assuming that you have a controller by the name Contact and you successfully extend the CI_Controller class, go to application/config folder and in config.php find:
$config['base_url'] = 'http://www.youdomain.com/';
Then in your internal links you should do:
Contact
If you are using javascript to make the redirect, put on top of the js file:
var host = 'http://www.yourdomain.com/';
Again:
window.location.href = host + 'contact';
If you're using codeigniter, you do not want to point to an .html file.
If you're using codeigniter correctly, you should use the helper methods that exist in codeigniter.
Instead of writing the anchor tag yourself, try this:
<?php echo anchor('contact', 'Contact'); ?>
to add the suffix to your controller in calling go to config/config.php and search for
$config['url_suffix'] = '';
and assign html to it to become
$config['url_suffix'] = 'html';

CodeIgniter -> Get current URL relative to base url

Tried URI::uri_string() but can't get it to work with the base_url.
URL: http://localhost/dropbox/derrek/shopredux/ahahaha/hihihi
Returns: dropbox/derrek/shopredux/ahahaha/hihihi
but http://localhost/dropbox/derrek/shopredux/ just returns an empty string.
I want the first call to return "ahahaha/hihihi" and the second to return "". Is there such a function?
// For current url
echo base_url(uri_string());
If url helper is loaded, use
current_url();
will be better
Try to use "uri" segments like:
$this->uri->segment(5); //To get 'ahahaha'
$this->uri->segment(6); //To get 'hihihi
form your first URL...You get '' from second URl also for segment(5),segment(6) also because they are empty.
Every segment function counts starts form localhost as '1' and symultaneous segments
For the parameter or without parameter URLs Use this :
Method 1:
$currentURL = current_url(); //for simple URL
$params = $_SERVER['QUERY_STRING']; //for parameters
$fullURL = $currentURL . '?' . $params; //full URL with parameter
Method 2:
$full_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Method 3:
base_url(uri_string());
I see that this post is old. But in version CI v3 here is the answer:
echo $this->uri->uri_string();
Thanks
//if you want to get parameter from url use:
parse_str($_SERVER['QUERY_STRING'], $_GET);
//then you can use:
if(isset($_GET["par"])){
echo $_GET["par"];
}
//if you want to get current page url use:
$current_url = current_url();
Running Latest Code Igniter 3.10
$this->load->helper('uri'); // or you can autoload it in config
print base_url($this->uri->uri_string());
I don't know if there is such a function, but with $this->uri->uri_to_assoc() you get an associative array from the $_GET parameters.
With this, and the controller you are in, you know how the URL looks like.
In you above URL this would mean you would be in the controller dropbox and the array would be something like this:
array("derrek" => "shopredux", "ahahaha" => "hihihi");
With this you should be able to make such a function on your own.
In CI v3, you can try:
function partial_uri($start = 0) {
return join('/',array_slice(get_instance()->uri->segment_array(), $start));
}
This will drop the number of URL segments specified by the $start argument. If your URL is http://localhost/dropbox/derrek/shopredux/ahahaha/hihihi, then:
partial_uri(3); # returns "ahahaha/hihihi"
you can use the some Codeigniter functions and some core functions and make combination to achieve your URL with query string.
I found solution of this problem.
base_url($this->uri->uri_string()).strrchr($_SERVER['REQUEST_URI'], "?");
and if you loaded URL helper so you can also do this current_url().strrchr($_SERVER['REQUEST_URI'], "?");
<?php $currentMenu = $this->uri->segment(2); ?>
<ul>
<li class="nav-item <?= ($currentMenu == 'dashboard') ? 'active' : '' ?>">
<a href="<?= site_url('/admin/dashboard'); ?>" class="nav-link"><i data-
feather="pie-chart"></i> Dashboard</a>
</li>
</ul
this is work for me

not able get post form submit values in codeigniter

I tried a simple form submit But I am not able to get the form values on controller using $this->input->post as well as $_POST[] methods. My view part is
<html>
<head>
<title> Feedback page</title>
</head>
<body>
<?php echo form_open('feedback/save'); ?>
<p>
<label>name: </label>
<?php echo form_input('name'); ?>
</p>
<p>
<label>Email: </label>
<?php echo form_input('email'); ?>
</p>
<p>
<label>Feedback: </label>
<?php echo form_textarea('feedback'); ?>
</p>
<p>
<?php echo form_submit('submit','Submit'); ?>
</p>
<?php echo form_close(); ?>
</body>
</html>
and controller part is
<?php
class Feedback extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model("MFeedback");
}
function index() {
$this->load->view('home/feedback_view.php');
//print "loaded";
}
function save() {
print "called";
print_r($this->input);
$name = $this->input->post('uname');
$email = $this->input->post('email');
$feedback = $this->input->post('feedback');
print $name . $email . $feedback;
$this->index();
}
}
?>
I am not sure what went wrong here or is there any config settings I need to look in to it.?
I have found out the problem. It is actually with the rewrite rule. Make sure you have rewrite rule like
RewriteEngine On
RewriteRule ^(application) - [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
on root folder of codeigniter.
Take a look at this:
http://codeigniter.com/user_guide/libraries/form_validation.html#validationrules
I had a similar issue when I started off using CI. You need to set at least one validation rule for the form and then check to see if the form submitted met that rule. You can then access the submitted form data like you are doing above..
It's been a while since I've used CI but something like this should solve your problem:
(Taken from the link above)
$this->load->library('form_validation');
$this->form_validation->set_rules('uname', 'Username', 'required');
$this->form_validation->set_rules('email', 'Password', 'required');
$this->form_validation->set_rules('feedback', 'Feedback', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
if ($this->form_validation->run() == FALSE)
{
// Here is where you do stuff when the submitted form is invalid.
$this->load->view('myform');
}
else
{
// Here is where you do stuff when the submitted form is valid.
print "called";
print_r($this->input);
$name = $this->input->post('uname');
$email = $this->input->post('email');
$feedback = $this->input->post('feedback');
print $name . $email . $feedback;
$this->index();
}
Hope that helps you in someway.. :)
your url address should be same as config->config.php->$config['base_url']
if your url address like
http://www.test.com
then your configh should be
$config['base_url'] = 'http://www.test.com/';
I was facing the same problem as you since the past half hour couldn't get anything to work. I tried your solution, it didn't help. But you were right it has to do with routing.
I was also passing other variables to my action like :
domain/controller/action/value1/value2
when I had my form submit data to :
domain/index.php/controller/action/value1/value2
it solved the problem. I am guessing if you pass values at the end the post variables don't work as expected. I know its supposed to work and I guess it does as well. Think its a problem with setting .htaccess correctly.
Thanks for the ideas that I solved my probs. I've got the same issue. My code worked well in WAMP, but when I moved to LAMP, I got all sorts of wired problems that I've never met before, and not getting any form post value was one of them.
According to the suggestion above:
I used form_open(index.php/controller/method) instead of form_open(controller/method) and it worked straight away.
However I got my index.php removed, and it's not shown in the address bar neither. As I said it's wired...
Use form action='domain/index.php?/controller/function name/parameter1/parameter2'
For example your domain is abc.com, controller is get, function name value,and parameter to be passed in functions are a and b
then just write the form action like following way.
<form action='http:/abc.com/index.php?/get/value/a/b' method='post' >
I solved my problem this way. Hope it will work for you.
Thanks
Firstly, In your view you've specified the name of your one input to be name, in your controller you're looking in post for uname.
Secondly, I don't remember if CodeIgniter does the same to $_POST but it definately destroys the $_GET array. If you want an associative array of all post inputs you can call this:
$this->input->post();
Thirdly, In a very very rare case your inputs might be getting blocked by XSS Filtering, you can stop this from happening by calling it like this (only for inspection purposes to see what's wrong, dont use this in production):
$this->input->post(NULL, FALSE);
If something is generally wrong, these calls will return FALSE, you should test for this using the === operator, as it will only match FALSE where == will match NULL as well.
Fourthly, You should test quickly using a simple html form, it looks like you're building your form right with the form helper but it never hurts to use a straightforward HTML Form for quick testing.
Other than that, you'll need to provide more information about your environment / configuration / generated html / etc... for us to figure out. You really didn't give us a lot to work with.
Well I have faced the same issue and following additions to .htaccess helped solved my problem.
<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
$data = array('id' => 'email',
'name' => 'email',
'class' => 'form-control');
echo form_input($data);
Just a quick mention that if you use an array to set up your inputs etc.. dont forget to include the name => 'your_desired_post_variable_name' in your array as this was my mistake, I was giving it just an id and wondering why my POST array was blank! Dont do the same! ;)
I've had a similar issue on my local ubuntu.
htaccess was properly configured but nothing inside post.
My issue was that apache didn't have mod rewrite enabled and I've fixed it by running these commands:
sudo a2enmod rewrite
sudo service apache2 restart
After that, all my post data went trough.
Hope that helps the next person who has the same issue

Resources