Codeigniter: How to capture slashes in route and pass to controller? - php

I'm trying to capture slashes from a url and pass them into my controller. Here's an example of my problem:
Let's say I GET http://localhost/here/is/an/example where my route looks like:
$routes['here/is/(:any)'] = 'my_controller/$1';
and I have the following function definition in my_controller:
public function index($rest_of_the_path) {
...
}
I would like the value of $rest_of_the_path to be 'an/example', but it actually equals 'an'. How can I structure my code so I get what I want?
P.S.: I'm using Codeigniter 2.1.3

You can simply do this:
public function index() {
$rest_of_the_path = implode("/", func_get_args());
...
}

Related

laravel legacy route url

i have 2 routes with POST methods
Route::post('/payment/checkOrder','Finance\PaymentCallbackController#checkOrder');
Route::post('/payment/paymentAviso', 'Finance\PaymentCallbackController#paymentAviso');
how can i create legacy links for these routes?
/plat.php?paysystem=5&method=checkOrder
/plat.php?paysystem=5&method=paymentAviso
You can have a single route that recieves a method string, and then call the desired functions according to it.
Route::post('/payment/{method}','Finance\PaymentCallbackController#handler');
// PaymentCallbackController.php
public function handler(Request $request){
// make sure to validate what methods get sent here
$this->{$request->method}($request);
// use $this if its in this controller, for otherControllers
// try something with the looks of app('App\Http\Controllers\OtherControllerController')->{$request->method}->($request);
}
Add this route:
Route::post('/plat.php', 'SomeController#action');
In your controller function:
// SomeController.php
public function someAction()
{
$paysystem = $request->query('paysystem');
$method = $request->query('method');
// some logic here
return view('something');
}

How call specific function dynamically that we get in url/path in laravel

My Code in route.php:-
Route::get('/register/{id}',array('uses'=>'UserRegistration#id));
I want to call function id (that can be any function of controller) in UserRegistration controller.
Url is like this:- http://localhost:8000/register/test,
http://localhost:8000/register/login
here test and login are function in controller.
{id} is the parameter you're passing to route. So, for your routes go with something like this:
Route::get('/register/id/{id}',array('uses'=>'UserRegistration#id));
//this route requires an id parameter
Route::get('/register/test',['uses'=>'UserRegistration#test]);
Doing this you can call your functions but it is not the recomended way of doing it. Having separete routes foreach function allows for a more in depth control.
public function id(Request $request)
{
return $this->{$request->id}($request);
}
public function test(Request $request)
{
return $request->all();
}

Read value from query string in CodeIgniter

I am new to CodeIgniter. I am trying to read values from query string in conventional method not segment.
This is my url.
http://localhost/Voyager/Main/UserActivation/?u=6df497644a10241cd89fad80f5c98496
Controller:
class Main extends CI_Controller{
public function UserActivation()
{
$hash=$this->input->get('u', TRUE);
log_message('debug', $hash, false);
$this->load->view('Main\view_userActivation');
}
}
I am trying to read value of 'u' in controller. But this isn't working. I am getting empty value in $hash variable.
Any help is appreciated.
Codeigniter works with URI Segments. You pass the values straight in your URL, separated with / and you grab them with positions after base_url like
$this->uri->segment(3)
Check this link: Codeigniter Documentation
There's a config option that unsets the $_GET array, but only if you have decided to change it.
This is how it looks by default in application/config/config.php:
$config['allow_get_array'] = TRUE;
If you've changed it to false - switch it back to true. Other than that, there's no reason why this wouldn't work, by default.
you can do something like this
Method 01
class Main extends CI_Controller{
public function UserActivation($u)
{
echo $u;
die;
$this->load->view('Main\view_userActivation');
}
}
Then URL should be
http://localhost/Voyager/Main/UserActivation/6df497644a10241cd89fad80f5c98496
Method 02
class Main extends CI_Controller{
public function UserActivation()
{
$value = $this->uri->segment(3);
echo $value;
$this->load->view('Main\view_userActivation');
}
}
Remove the / from the last uri segment.
http://localhost/Voyager/Main/UserActivation?u=6df497644a10241cd89fad80f5c98496
Also, another thing. Why you have your uri controller and methods starting in uppercase? That shouldn't be that way.

Laravel - can I control routes by rule?

So I have a Laravel Controller (MainController.php) with the following lines:
...
public function _settings_a(){
return view('_settings_a');
}
public function _settings_b(){
return view('_settings_b');
}
public function _settings_c(){
return view('_settings_c');
}
public function _settings_d(){
return view('_settings_d');
}
public function _staff_a(){
return view('_staff_a');
}
public function _staff_b(){
return view('_staff_b');
}
public function _staff_c(){
return view('_staff_c');
}
...
And my routes.php is as follows:
Route::any('_staff_a''MainController#_staff_a');
Route::any('_staff_b''MainController#_staff_b');
...
etc.
It seems there are a LOT of lines and a LOT of things to change if I change my mind...
I was wondering if I can have some regex in routes.php and an equivalent regex in MainController.php for handling routes that begin with an underscore (_)?
Can any Laravel experts share some tips/suggestions? I'm quite new to the framework.
Sure - just add it as a parameter. E.g. like this:
Route::any('_staff_{version}', 'MainController#_staff');
public function _staff($version) {
return view('_staff_'.$version);
}
I don't think you need to mess with regex. You can use implicit controllers Route::controller() which isn't the BEST solution, but will do what I think you are wanting.
So instead of
Route::any(..)
you can do
Route::controller('url', 'MainController');
So your route to whatever 'url' is will send you to this controller. Follow that with a '/' and then add whichever method in the controller you want to call.
Here is an example:
My url: 'http://www.example.com/users'
// routes.php
Route::controller('users', UserController');
// UserController.php
public function getIndex()
{
// index stuff
}
Now I send a request like: http://www.example.com/users/edit-user/125
// UserController.php
public function getEditUser($user_id)
{
// getEditUser, postEditUser, anyEditUser can be called on /users/edit-user
// and 125 is the parameter pasted to it
}
Doing it this way should allow you to be able to just send a request (post or get) to a url and the controller should be able to call the correct method depending on the url.
Here are some more rules about it: http://laravel.com/docs/5.1/controllers#implicit-controllers

Custom URI routing by query string with CodeIgniter?

I am looking to make a custom route using the CodeIgniter framework. I am trying to make the URL like so:
http://localhost/accounts/Auth.dll?signin
So far I have tried adding the following to my routes.php config file:
$route['accounts/Auth.dll?signin'] = "accounts/signin";
but as you would guess, it doesn't work. I have also tried escaping the characters like this:
$route['accounts/Auth\.dll\?signin'] = "accounts/signin";
and that doesn't work either. I've also tried including the leading and trailing slashes .. that didn't work either. Anyone know by chance what could solve my issue?
I highly recommend to use a SEF routing.
But if for any reason you're not eager to, you could check the query string inside the Accounts Controller, and then invoke the proper method, as follows:
Router:
$route['accounts/Auth.dll'] = "accounts";
Controller:
class Accounts extends CI_Controller
{
public function __construct()
{
# Call the CI_Controller constructor
parent::__construct();
# Fetch the query string
if ($method = $this->input->server('QUERY_STRING', TRUE)) {
# Check whether the method exists
if (method_exists($this, $method)) {
# Invoke the method
call_user_func(array($this, $method));
}
}
}
protected function signin()
{
# Your logic here
}
}
This allows you to invoke the methods by query string automatically.
I am not sure, that its okay to use GET-params in routes.php config.
Try such way:
routes.php
$route['accounts/Auth.dll'] = "accounts/index";
accounts.php
public function index() {
if ($this->input->get('signin') != false) {
$this->signin();
}
}
private function signin() {
// some code
}
But, as for me, it's bad way.
I recommend you just use another routing:
/accounts/Auth.dll/signin
And etc.

Categories