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.
Related
Suppose I have this controller:
class Site extends CI_Controller {
public function __constructor()
{
parent::__constructor();
echo 'test';
}
public function index()
{
$view['site'] = true;
$view['view_home'] = lang('home');
$view['view_home_url'] = base_url();
$view['view_name'] = lang('home');
$view['content'] = 'site/home';
$this->load->view('partials/template', $view);
}
public function association()
{
echo 'test';
}
}
when I type: http://localhost/mysite/association I get 404. Essentially I would like to use the same controller Site which is the default controller, to load multiple routers. How can I do this?
You are using codeigniter and you can't access url lik below as you haven't defined any routes for that. http://localhost/mysite/association . You will have to go url like
http://localhost/mysite/site/association //sometimes need index.php
if you don't configure it. first parameter is the controller and second one is a method
For going to your custom URL you will have to go to application/config/routes.php and define your new custom
route $route['association'] = 'site/association';
You can also define like below routes and access from browser
$route['association/url1/url2/url2'] = 'site/association'; //simple
Hope that you have got your answer
I have several routes in web.php:
Route::get('/','PagesController#index');
Route::get('/contact','PagesController#contact');
and so on...
I need to get in my PagesController the current 'module' (index, contact or smth else).
The Controller's code:
class PagesController extends Controller
{
public function index()
{
$menu = new Menu();
$links = $menu->getMenu();
//$header=url()->current(); // returns the full url, e.g. http://test.com/
//$header=Route::current(); // error: Class Route not found
return view("index",['links'=>$links,'header'=>$header]);
}
}
For example, $header should be equal to "/" inside PagesController#index and $header = "contact" inside PagesController#contact.
I need the universal solution for all the modules I'll have in future.
Thanks a lot!
I have not had a chance to test this, however you should be able to achieve this with the following:
Route::get('/{page?}', function($page = 'index') {
return app('App\Http\Controllers\PageController')->{$page}($page);
});
Basically, this is setting a route to have an optional $page variable. If no page name is passed (e.g. contact), we default to index.
We finally use the app() helper to call the PageController and then use the ->{$page}() syntax to call a dynamic controller method (In the default case index).
Hope this helps.
So, lets say I have a global view and specific view.
In global view, the url may look like this (http://example.com/index.php/controller/method/)
Where when it come to the specific page view, the url will turn like this:
(http://example.com/index.php/controller/method/1989-03-25)
The difference between the global view and the specific page view is, if in the global view it shows the information in general, but in the specific page view it shows based on the detail or the date.
Of course, not only have different view, but also they will have different function of models.
The point is, I just want to make the url keep in order (which it mean there is no change in the name of the controller method).
How to do this. Thanks in advance.
You create just one param into your function. And set the param value is null. like this
class YOUR_CLASS extends CI_controller {
public function method($params=null) //null is Empty value
{
if($params)
{
//load your modal and view with your param
}
else
{
//load your modal and view
}
}
}
This method supports the following type of URL's without any issue.
http://example.com/index.php/YOUR_CLASS/method/
http://example.com/index.php/YOUR_CLASS/method/1989-03-25
Hope this will help you....
This class used to wrap CI_controller, my_base_controller will override CI_controller methods for depends to your project needs
class my_base_controller extends CI_controller {
}
You can load models by known states and define it in my_base_controller class.
class my_controller extends my_base_controller{
public function method($params=null) //null is option value
{
}
}
Good luck!
You can add additional parameter in your method like:
class Your_controller extends CI_controller {
public function method($params = null)
{
// Your Code Here with the condition for processing of the $params variable
}
}
in which that $params can be something in your URL like:
http://example.com/controller/method/your-dynamic-params
So if the $params is null you will call the model the views the general and if the $params has a specific value you can call other model by using if or switch conditional statements. Hope this helps...
Update with Example
you can use the $params variable like this:
if ($params == "1991") {
$this->load->view('general.html', $params);
} elseif ($params == "1992") {
$this->load->view('year_1992.html', $params);
} else {
$this->load->view('other_years.html', $params)
}
in this way you can use the $params as a conditional variable to load different views.
or using switch:
switch($params) {
case '1991':
$this->load->view('general.html', $params);
break;
case '1992':
$this->load->view('year_1992.html', $params);
break;
default:
$this->load->view('other_years.html', $params)
}
Note: Use a helper method so you can avoid fat controllers because it will be hard to test your methods if you have a several lines of codes in a function.
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.
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());
...
}