How to prepare route to get controller ,method names from url - codeigniter

I am trying to get controller , method names from url and redirect to particular route.
This is for recent codeigniter version 4.
$routes->add('(:any)/(:any)','$1::$2');
For suppose :- domain/text1/text2 should redirect to text2 method of text1 controller.

make route for it:config/routes.php
$route['text1/(:any)/(:any)']='controller_name/method_name/$1/$2';

Related

How to use multiple controllers in CodeIgniter?

I need to use 2 controllers in CodeIgniter 3. I have Welcome and Paypal controllers. In routing, previously I had following code:
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Then to add Paypal, I tried following codes, which were not helpful:
//$route['(:any)'] = 'paypal/index';
//$route['Paypal'] = 'paypal/index';
//$route['Welcome/Paypal/(:any)'] = 'welcome/index';
I tried them separately and also together but still I get this result:
404 Page Not Found
The page you requested was not found.
What I need to write, for using my Paypal controller as well?
If, for example, you want to go to the URL https://example.com/paypal you do not need a route if the Paypal controller has an index function.
If you wanted a way to "buy" a pair of socks that used the URL https://example.com/buy/socks but wanted to handle this request using the PayPal controller method buy($item), then you need a $route.
$route['buy/(:any)'] = 'paypal/buy/$1';
But you do not need a route if your "buy" URL is https://example.com/paypal/buy/socks
The only time you need to define a $route is when you want to deviate from CodeIgniter's controller/function[/arg1[/arg2[...]] URI pattern.
Your problems may not be route related. Be certain you followed the CodeIgniter rules for controller file and class naming? The name of the file must begin with an uppercase letter, i.e. Paypal.php and the class definition must match the file name exactly. i.e.
class Paypal extends CI_Controller {
What i got from your question is that you want the paypal controller to be rerouted or redirected to welcome controller.
if i got it right, you can simply do that with redirect .. in your paypal controller use redirect in your construct or index to redirect you to welcome controller

How to custom API URL on 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.

Codeigniter router get parameter

I use $.getJSON() to retrieve some data for a couple of cascading dropdowns in my form. $.getJSON() automatically appends the parameter at the end of the URL like domain.com/controller/method/?parent=5
So, I've declared my method like public function method($parent) which works file, but the same method will be used from other parts of the website that will call it like domain.com/controller/method/5
I tried to create a route in routes.php like the one below:
$route['business/regions/?parent=(:num)'] = 'business/regions/$1';
but it doesn't seem to work. Am I doing something wrong? Maybe ? is confusing the regex parser of the router? Do I have to escape it somehow to make it a 'literal' ? ?
Or is it that router is not used to 'rewrite' get parameters at all? I'm very confused, as it should work but it doesn't and I'm wondering what's wrong with it...
Codeigniter route parameters are for url parameters. It is particularly useful when trying to create a REST styled url pattern.
What you're trying to do is get url query string from the url which is not supported via the Codeigniter router. For you to get what you want you can do the following:
In your routes.php:
$route['business/regions'] = 'business/regions';
and in your controller Business.php:
public function regions() {
//the numeric id you're looking for
$parent = $this->input->get('parent');
}

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());
}

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

Resources