_remap function in codeigniter - php

I'm trying to use remap function in codeigniter but it doesn't work. I have a method called submit_me and I would transform it in submit-me in the URL. I read I can use _remap function but unfortunately I wasn't able to use it.
public function _remap($method)
{
if($method == 'submit-me')
{
$this->submit_me();
}
else
{
$this->index();
}
}
this is the correct use of it?

_remap() is used for a call a category.
Example :
I’m building a website for a TV production company. A section was needed to showcase their productions. These productions fall into categories: factual, drama, events, kids and co-productions.
the segment of the url after the controller name is automatically passed as the argument
function _remap($method){
if($method == 'current' ||
$method == 'factual' ||
$method == 'kids' ||
$method == 'drama' ||
$method == 'events' ||
$method == 'co')
{
I use segment(4) here as I'm using the URI language class, which adds an extra segment before the controller, so usually segment(3) would be OK
$this->genre($method, $this->uri->segment(4));
}else{
$this->index();
}
}
function index(){
redirect('productions/current');
}

Related

Getting the url where the request is redirected from in yii2?

Sorry for my English, but what I'm trying to say is explained below.
I have a controller say ControllerCard which has an action like this.
function actionScanCard()
{
...
$this->redirect('/transaction/redeem');
...
}
In other controllers, ControllerTransaction, I am trying to get that it comes/redirected from /card/scan-card
function actionRedeem()
{
$redirectFrom = ????;
if ($redirectFrom === '/card/scan-card')
{
// some actions
}
else
throw new ForbiddenHttpException('Must scan card!');
}
How do I get this $redirectFrom value with Yii2?
You could use the remember() & previous() methods in yii\helpers\BaseUrl.
function actionScanCard()
{
...
\yii\helpers\Url::remember();
$this->redirect('/transaction/redeem');
...
}
in TransactionController (or other)
function actionRedeem()
{
$url = \yii\helpers\Url::previous();
if($url === Url::to('card/scan-card')) {
// some actions
} else{}
}

Can we multiple condition in PHP?

I am front of reflexion about my php developments. I'm trying to optimize my code.
I have often condition like this :
if($userConnected->getType() == User::BUYER_ACCOUNT_TYPE || $userConnected->getType() == User::ADMIN_ACCOUNT_TYPE){//Mycode}
My question is : Is it possible to have something like this :
if($userConnected->getType() == User::BUYER_ACCOUNT_TYPE || User::ADMIN_ACCOUNT_TYPE)
Actually the best way I found to do this is :
if(in_array($userConnected->getType(), array(User::BUYER_ACCOUNT_TYPE, User::ADMIN_ACCOUNT_TYPE)))
And I want to know if there is a better way ?
Thank you in advance
Thomas
You can add some public methods to your User class to check if the user is a buyer or an admin:
public function isBuyer()
{
return $this->type === self::BUYER_ACCOUNT_TYPE;
}
public function isAdmin()
{
return $this->type === self::ADMIN_ACCOUNT_TYPE;
}
Having these methods you can simply check:
if ($userConnected->isBuyer() || $userConnected->isAdmin())
You can go further and do a single method if the condition above is used very often:
public function isAllowed() // just an example of a method name
{
return $this->isBuyer() || $this->isAdmin();
}

Codeigniter passing parameter to controller index

I'm building a tutorialsystem with codeigniter and would like to achieve the following URL structure:
/tutorials --> an introduction page with the list of all the categories
/tutorials/{a category as string} --> this will give a list of tutorials for the given category, e.g. /tutorials/php
/tutorials/{a category as string}/{an ID}/{tutorial slug} --> this will show the tutorial, e.g. /tutorials/php/123/how-to-use-functions
/tutorials/add --> page to add a new tutorial
The problem is that when I want to use the first two types of URLs, I'd need to pass parameters to the index function of the controller. The first parameter is the optional category, the second is the optional tutorial ID. I've did some research before I posted, so I found out that I could add a route like tutorials/(:any), but the problem is that this route would pass add as a parameter too when using the last URL (/tutorials/add).
Any ideas how I can make this happen?
Your routing rules could be in this order:
$route['tutorials/add'] = "tutorials/add"; //assuming you have an add() method
$route['tutorials/(:any)'] = "tutorials/index"; //this will comply with anything which is not tutorials/add
Then in your controller's index() method you should be able to work out whether it's the category or tutorial ID is being passed!
I do think that a remap must be of more use to your problem in case you want to add more methods to your controller, not just 'add'. This should do the task:
function _remap($method)
{
if (method_exists($this, $method))
{
$this->$method();
}
else {
$this->index($method);
}
}
A few minutes after posting, I think I've found a possible solution for this. (Shame on me).
In pseudo code:
public function index($cat = FALSE, $id = FALSE)
{
if($cat !== FALSE) {
if($cat === 'add') {
$this->add();
} else {
if($id !== FALSE) {
// Fetch the tutorial
} else {
// Fetch the tutorials for category $cat
}
}
} else {
// Show the overview
}
}
Feedback for this solution is welcome!

How do you show a 404 when there are extra URL segments in CodeIgniter?

Basically, I have a CodeIgniter site that registers users through this url:
http://website.com/user/register/1
Unfortunately, when you pass extra arguments in the URL like this:
http://website.com/user/register/1/extraargs/extraargs
Here is my code:
<?php
class User extends CI_Controller
{
public function register($step = NULL)
{
if (($step!=NULL)&&($step == 1 || $step == 2 || $step == 3)) {
if ($step == 1){
$this->load->view('registration_step1');
}
} else {
show_404();
}
}
}
?>
It doesn't show a 404. It simply ignores the extra arguments. I want it so that the extra arguments wouldn't affect the URL. I want to show a 404 if there are extra URL arguments. How do you do this? Also, can the extra URL segments affect security? Thanks.
Not sure why you'd want to do that really, but you could use func_num_args() and validate from that, i.e.,
public function register($step = NULL)
{
if ( func_num_args() > 1 ) show_404();
}

Codeigniter - Checking if a GET request is made

Im using CodeIgniter to write a site ... I understand $_GET requests are now used like so www.website.com/function/value .. and in the controller getting a url segment is written like so:
$userId = $this->uri->segment(3, 0);
Im just wondering, when a controller loads, i want to check if there is any uri segments, if there is then push to one view, else if there isnt a uri segment push to another.
Is that possible?
cheers.
You can use your controller arguments for that too.
When accessing /user/profile/1 your controller named User will call the method profile() and pass the number 1 as the first argument to your method. Like so:
class User extends CI_Controller {
{
public function index()
{
$this->load->view("user_index");
}
public function profile ( $userId = null )
{
if( (int)$userId > 0 )
$this->load->view("user_profile");
else
$this->load->view("another_view");
}
}
This is a very basic sample and I'm just trying to show the idea.
Seems like your asking two questions...
First, to check if the request is get
public function get_test()
{
if($_SERVER['REQUEST_METHOD'] == "GET")
{
//do something from get
echo "GET";
}
else
{
//do something not get
echo "NOT GET";
}
}
The next question seemed to be checking uri segments
public function get_test()
{
if($_SERVER['REQUEST_METHOD'] == "GET")
{
//do something from get
//echo "GET";
if($this->uri->segment(3)) //is true as is not empty
{
echo $this->uri->segment(3);
}
else
{
echo "I am nothing without my URI Segment";
}
}
else
{
//do something not get
echo "NOT GET";
}
}
As I understand you can use PHP default value.
function myFunction($var1 = NULL) {... if($var1 === NULL) ...}
Now if you do not pass the param you will get the NULL value.
I am still not using version 2 of codeigniter but this framework do not accept get requests; unless you mess with the configuration. Theres a function $this->input->get('myGet') you should look around at de the codeigniter.com/user_guide

Categories