How to custom API URL on codeigniter - codeigniter

I have a PHP CodeIgniter Controller with name index and have a method that get details of id kode ($kode) using API get method.
Now when i need to show kode data for example for id AALI
I call this URL
http://www.example.com/?q=AALI
My target
How to make user data accessible by next URLs
http://www.example.com/AALI
I've try using function _remap on code Igniter, but it still wont work.

Have a look at Codeigniter URLs
As per your statement, your controller name is index and there would be an index function in your controller which renders the default view. it means you have changed default_controller to index in your Config.php
Now if you read the link above about Codeigniter URLs, there is a way to get data which passed in the URL after "/" You have to load url helper you can either autoload(recommended) it or load it in your constructor or Controller as per your convenience
Then you can just type
$param=$this->uri->segment(2); // in case your URL is http://www.example.com/AALI
The first segment is controller itself, The second is the function if your url is complete and the third is the parameter in CI URL structure but if you are not providing function name the first segment will always be your controller . So the second is your parameter. Just save it in a variable and do what you like.

Related

Whats the difference between redirect and this in Codeigniter?

I am new in Codeigniter and it's one of the good frameworks of php. But on some conditions I'm confused. Like this one. If any of you have any clarification about my dough, it's a great help for me.
Offcouse redirects refresh the page and $this not but apart from this I want to know - anyhow both of them used to go to somewhere else on view pages or like in other controller or in same controller to other methods.
But we don't use these side by side because when getting any of them it will go to that page or method without checking the next lines.
In case of a normal difference then have lot's of but I just want to know about the condition of going to next page or method when we use redirect or $this like this -
$this->Function($value); //It's method of same controller.
redirect('Controller/function'); //It's also doing same with page reload.
Thank for looking my problem.
Redirect()
When you will call any function of helper in codeigniter then you can call function directly without using any object. Helper in Codeigniter is collection of functions.
Redirect() method is a part of URL helper in Codeigniter.
For your ref. https://www.codeigniter.com/user_guide/helpers/url_helper.html
So, just load helper using $this->load->helper('url'); or you can also mention in autoload.php file.
$this->Function(); used to call a function from same controller
$this->Function(); used to call a function from same controller
redirect()
While building a web application, we often need to redirect the user from one page to another page. CodeIgniter makes this job easy for us. The redirect() function is used for this purpose.
redirect($uri = '', $method = 'auto', $code = NULL)
The first argument can have two types of URI. We can pass full site URL or URI segments to the controller you want to direct.
The second optional parameter can have any of the three values from auto, location or refresh. The default is auto.
The third optional parameter is only available with location redirects and it allows you to send specific HTTP response code.
Redirect means jumping to another function mentioned in the redirect method.
$this->Function($value); => jumping to another function and you can execute the code of the same function as well as pass the value back by returning value.
When you send request to codeigniter generally CI controller gets called and then function which is mentioned in uri segment. like below... So this will be another request.
redirect('Controller/function'); //It's also doing same with page reload.
But when you have to call another function within the same request then you can use below approach
$this->Function($value); //It's method of same controller.
This will execute the given function and return the value within same request.

Getting the controller for a form in magento

I have this action defined in my form.phtml (Environment is magento).
form action="getUrl('contacts/index/post')
Now I need to know which controller is getting called so I can get the values in the backend.
That is pretty straight forward.
The first word contacts is the front-name which in magento standards is the module name. The second word index is the controller file name and the last word post is the action of the controller.
Because thats a core controller, you can find the file under /app/code/core/Mage/Contacts/controllers/IndexController.php
Look at the postAction() function.

Magento - passing params in URL to template file

I have a template file at ../page/video.phtml and it's served at http://mysite/video.
I want to add params in the url to play different videos on that page. I can add it as a query string param, http://mysite/video?select=filename but I would prefer to use http://mysite/video/filename.
However, when I try this I get a 404. What would I have to do to achieve this?
I'm using Magento 1.7
You must explicitly include all the action parts (route, controller and action) in the URL before adding parameters this way, because when you use http://mysite/video/filename, Magento looks for the index action of the filename controller for the module having a front route named video (which does not exist, hence the 404 error).
From the URL you gave, a working URL would rather look like this : http://mysite/video/index/index/select/filename

Pass hidden value in rewritten URL?

Just wondering if there is a way to pass a hidden variable in a rewritten URL?
My links are working fine with this format:
http://www.mydomain.com/city/state/businessname/
But I'd also like to be able to get and pass the businessID value, but not have it show up in the rewritten link.
http://www.mydomain.com/city/state/businessname/busid/ <- hide busid
If the businessId should not appear in the frontend url then you can retrieve it in the controller / action that is handling this request.
ie. If you can get the businessid from the businessname, there is no need to retrieve it and then rewrite url, just do that logic in your controller action.
Or setup a Filter or Interceptor to handle it.

Codeigniter parameters

I've read the URI parameters user guide and still have a question:
http://codeigniter.com/user_guide/general/routing.html
With the following:
{http://myapp/locations/1} I get a 404 error...
{http://myapp/locations} appropriately executes the index() function in the Main controller
{http://myapp/locations/main/locations/1} works, and the value is passed properly to index($var)
I do have other functions in Main.
How is it possible to get the first line to work in order to clean URL's?
Thanks in advance,
Alan
CodeIgniter reads an url as domain/controller_name/method-name/method_parameters and in your first url here http://myapp/locations/1 the first portion (myapp) is your domain name, second (locations) is your controller name and the third portion should be controller's method name and in this case you've passed 1 and obviously there's no such a method name, so it's showing error.
if you pass domain/controller_name like you did here in this url http://myapp/locations, then CodeIgniter reads the first portion as the domain_name and the second portion as controller_name and when there is no third portion in the url then CodeIgniter calls the index method/function by default, so your second url is working.
In your last url you have http://myapp/locations/main/locations/1 and it's been read as
myapp-domain name
locations-controller name
main-method/function name
and rest of all are passed as main controller's arguments. So remember that, the third part of an url is method/function name and if third part is not given then CodeIgniter calls the index method by default and in that case you have to declare a default index method/function in that controller, otherwise an error will be occured.

Resources