Can we multiple condition in PHP? - 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();
}

Related

Laravel option only for the admin (permission)

I write a method, that which checks whether the user is an administrator in file UserController.php:
public function create(){
$chackIsAdmin = Auth::user()->permissions;
if ($chackIsAdmin === 1) {
return view('users.adduser');
} else {
return redirect('warehouse');
}
In table "users" I have column "permissions". Each user is assigned a number 1 or 0. 1 - is admin, 0 is NOT an admin.
I wrote also instruction if, which displays option "Add user" only the user administrator:
#if (Auth::user()->permissions === 1)
<li>Add new user</li>
#endif
It all works correctly, but I wonder whether in Laravel I can do it in a different way ??
Whether my way is safe?
I think the better way is to write the function on the User model.
class User extends Model
{
public function isAdmin()
{
return $this->attributes['permissions'] == 1;
}
}
Then it's very easy to use...
return Auth::user()->isAdmin() ? view('users.adduser') : redirect('warehouse');

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

_remap function in codeigniter

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

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

passing data into a function

Okay So I know this is super basic and I should know how to do this but I'm blanking and having trouble finding the answer in Google. I have an include that has an array of variables for example
$phrases["text"][1] = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?";
$phrases["mp3"][1] = "http://example.com/file.mp3";
Then a function that gets the varibles:
function return_phrase($phrase_name="", $fallback="",$default ="text"){
$next= (isset($default) && $default =="mp3") ? 'text' : 'mp3';
if(isset($tts_phrases[$default][$phrase_name])){
return $phrases[$default][$phrase_name]);
}
else if(isset($tts_phrases[$next][$phrase_name])){
return $phrases[$next][$phrase_name]);
}
else{
return $fallback;
}
}
The problem is that the $phrases arrays aren't being sent to the function I can include the file in the function itself but I know that's the wrong way to do it. I think I need to use $global just not sure how.
Method 1: Pass $phrases, $tts_phrases as parameters
function return_phrase(array $phrases, array $ttphrases, $phrase_name="", $fallback="",$default ="text"){
$next= (isset($default) && $default =="mp3") ? 'text' : 'mp3';
if(isset($tts_phrases[$default][$phrase_name])){
return $phrases[$default][$phrase_name]);
}
else if(isset($tts_phrases[$next][$phrase_name])){
return $phrases[$next][$phrase_name]);
}
else{
return $fallback;
}
}
Method 2: Make $phrases, $tts_phrases global (bad!)
function return_phrase($phrase_name="", $fallback="",$default ="text"){
global $phrases, $tts_phrases;
$next= (isset($default) && $default =="mp3") ? 'text' : 'mp3';
if(isset($tts_phrases[$default][$phrase_name])){
return $phrases[$default][$phrase_name]);
}
else if(isset($tts_phrases[$next][$phrase_name])){
return $phrases[$next][$phrase_name]);
}
else{
return $fallback;
}
}
Using global variables is a quick and easy fix, but once your application gets larger they become very hard to keep track of. For example, take this legacy code snippet that I have to deal with at work:
function foo() {
global $mysqldsn, $ldapdsn, $autologout_timer, $isMobileDevice, logout_fail_limit, $logout_fail_window, $lang, $project_contact_email, $project_contact_name ... (50 or 60 more global variables following)
...
}
Any time I am looking at one of the pages that just pulls one of those variables out of thin air, I have to Ctrl+F the whole project and make sure that every little change hasn't messed up the whole app. When you keep your variables in local scope, you know exactly what you are changing.

Categories