Redirection In index.php page in Codeigniter - codeigniter

Iam developing a site in codeigniter.This is my url http://myserver.net/visio/UBwXo.
Here http://myserver.net/visio/ is my base_url.
After /visio/ here have a variable.When there have any value after /visio/ then i wantto take the corresponding url from database to this value.
That means in my database,
UBwXo => "*any url***"
jshom => "*any url***"
So when getting value after /visio/ i want to take the corresponding url from databse and redirect it in to that url without using htaccess.
I want to done this redirection process in index.php page of root folder.
Is this possible?
The orginal url for http://myserver.net/visio/UBwXo like myserver.net/visio/index.php/admin/index/UBwXo
the default controller is admin

First, create redirect.php file in the controllers folder (application/controllers) and add this code to this file:
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Redirect extends CI_Controller
{
/**
* Method to redirect from an alias to a full URL
*/
public function index()
{
$alias = $this->uri->segment(1);
$this->db->select('url');
$query = $this->db->get_where('links', array('alias' => $alias), 1, 0);
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$this->load->helper('url');
redirect($row->url, 'refresh', 301);
}
}
else
{
echo "Sorry, alias '$alias' not found";
}
}
}
Then create table in your database. Your table must be like this:
CREATE TABLE IF NOT EXISTS `links` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`alias` varchar(6) CHARACTER SET utf8 DEFAULT NULL,
`url` text CHARACTER SET utf8,
PRIMARY KEY (`id`),
UNIQUE KEY `alias` (`alias`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
After that, set default controller value to the redirect class.
Open application/config/routes.php. Find $route['default_controller'] , then set redirect as a value to this variable, like this:
$route['default_controller'] = "redirect";
Then enjoy life ;)
EDIT:
I had forgotten to mention URI routing in config/routes.php for redirecting:
$route[':any'] = "redirect/index/$1";

Your best resource here will be the CodeIgniter guides, specifically the page on Controllers. The section on Remapping Function calls should be exactly what you need in this case.
Since the default behavior is to look for a controller method with the name of the first segment after the base url, we need to change this to pass it as an argument to some function. Your controller might look something like this:
class Shortener extends CI_Controller {
private function shorten( $token ){
// Find the URL that belongs to the token and redirect
}
public function _remap( $method, $params = array() ) {
// Catch other controller methods, pulled from the CodeIgniter docs
if ( method_exists( $this, $method ) ) {
return call_user_func_array( array( $this, $method ), $params );
}
$this->shorten( $method );
}
}

Related

how to hide id from url in Laravel 6?

hide id from url
https://wallpaperaccess.in/photo/162/download-wallpaper
i want url like this
https://wallpaperaccess.in/photo/download-wallpaper
ImagesController.php
public function show($id, $slug = null ) {
$response = Images::findOrFail($id);
$uri = $this->request->path();
if( str_slug( $response->title ) == '' ) {
$slugUrl = '';
} else {
$slugUrl = '/'.str_slug( $response->title );
}
$url_image = 'photo/'.$response->id.$slugUrl;
//<<<-- * Redirect the user real page * -->>>
$uriImage = $this->request->path();
$uriCanonical = $url_image;
if( $uriImage != $uriCanonical ) {
return redirect($uriCanonical);
}
Route
// Photo Details
Route::get('photo/{id}/{slug?}','ImagesController#show');
NOTE: i don't have any slug column in database, so can we use title as slug?
You should add a column field slug and auto-generate it from title
use Illuminate\Support\Str;
$slug = Str::slug($request->input('title'), '-');
In Models\Image.php
public function getRouteKeyName()
{
return 'slug';
}
In routes\web.php
Route::get('photo/{image:slug}','ImagesController#show');
In app\Http\Controllers\ImagesController.php
use app\Models\Image.php;
...
public function show(Image $image)
{
// controller will automatically find $image with slug in url
// $image_id = $image->id;
return view('your.view', ['image' => $image]);
}
In order to use a slug in the URL instead of an id, you'll need to...
Create a column in your table where you store the slug. A good way to make a slug unique is to append the actual id at the end. If you really don't want to see the id anywhere, you have no choice, you'll have to ensure the slug is unique yourself (there are a lot of ways to achieve this).
This is one way to automatically create an unique slug:
Make sure the slug column is nullable, then open your model and add these methods.
They are called "model events".
created is called when the model is, well, created.
updating is called when you are updating the model but before it's actually updated.
Using created and updating should automatically create the slug when you create or update a Images instance.
protected static function booted()
{
parent::booted();
static::created(function (Images $images) {
$images->slug = Str::slug($images->title.'-'.$images->id);
$images->save();
});
static::updating(function (Images $images) {
$images->slug = Str::slug($images->title.'-'.$images->id);
});
}
From a SEO point of view, updating the slug when the title change is arguably not a good practice, so you might want to omit this part (static::updating...), it's up to you.
Go to your model and add the following method:
/**
* Get the route key for the model.
*
* #return string
*/
public function getRouteKeyName()
{
return 'slug'; //or whatever you call the slug column
}
This way, the router will resolve your model by the slug, not the id.
In your route file, remove the id and change the name of the slug to match the name of your model:
Route::get('photo/{images}','ImagesController#show'); //here I'm assuming your model is Images from what I see in your controller
In your controller, change the declaration of your show method to this:
public function show(Images $images)
{
dd($images);
// if you did all this correctly, $images should be the Images corresponding to the slug in the url.
// if you did something wrong, $images will be an empty Images instance
//
//
// your code...
}
Images should be renamed to Image, models should not be plural. However, it should not make any difference here.

Laravel session won't change

I'm trying to change website language using the "site_lang" session key. Here's the Controller
public function __construct()
{
if( !session()->has( 'site_lang' ) ) { session(['site_lang' => 'en']); }
$this->lang = session()->get('site_lang');
app()->setLocale($this->lang);
User clicks another language link to change website lang so he gets redirected to this function in the HomeController which extends Controller
session()->forget('site_lang');
session(['site_lang' => $lang]);
//echo session()->get('site_lang').'<br>'; //prints fr
$this->lang = $lang;
//echo $this->lang; //prints fr
app()->setLocale($lang);
//echo app()->getLocale(); //prints fr
return redirect("/");
Removed some validations to make it brief
So the user clicks "French" and gets redirected to "HomeController#change_lang" which is the previous function and all echo statements prints fr but when the user gets redirected to the home page the value gets assigned again to en by this line
if( !session()->has( 'site_lang' ) ) { session(['site_lang' => 'en']); }
Why does this happen and how to solve it?
SESSION can't be accessed from the constructor unless following a work around like laravel - Can't get session in controller constructor

Rewrite the url using CodeIgniter

I read a lot of forum topics but I did not found any answer which meets my needs.
I am running a blog system and my current urls for each article look like: www.example.com/controller/method/id. However, for each article I have a title stored in a database, and would like to use that title in the url, so it would look like this: www.example.com/title
Hello there you are asking for a whole lot of code and you did not do any research yourself.
The best way is to use id with your title: http://www.example.com/id/long-title it is much better than using only title, because:
same title can occur more than once (creates problem that can be avoided)
slow loading/searching due querying by slug instead of ID (slow performance)
user needs to remember whole title (with id+title; user can copy partially broken url www.example.com/234593/this-title-is-) / opinion based
In order to make my proposal work you need to:
set up routes (application/config/routes.php)
//leave two CodeIgniter's routes on top
$route['default_controller'] = "welcome";
$route['404_override'] = '';
//leave this two routes in this order + make them LAST routes
$route['(:num)'] = 'blog/post/$1'; //if only id is in url
$route['(:num)/(:any)'] = 'blog/post/$1/$2'; //if id and title is in url
set up controller (application/controllers/blog.php)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Blog extends CI_Controller {
public function __construct()
{
parent::__construct();
//load models helpers etc if not autoloaded
$this->load->helper('url');
}
public function index()
{
echo 'should throw 404 or redirect home because id was not provided';
}
public function post($id = null, $title = '')
{
if (!$this->validId( $id )) redirect(); //redirect somewhere because id of post is invalid search/404/home...
if (empty(trim($title))) {
redirect(base_url().$id.'/'.$this->getTitle($id), 'location', 301); //redirect to same page search engines friendly (301 header)
}
//display what you need
echo 'params; id: ' . $id . ' title: ' . $title;
}
private function validId($id)
{
if (is_numeric($id))
return true; //use database to check validity in this case every id is valid
}
private function getTitle()
{
//id should be good to use at this point
//get title using database
return $this->seoUrl('How you react of crying babies will determine their future.'); //made up title
}
private function seoUrl($string)
{
//source: http://stackoverflow.com/a/11330527/1564365
//Lower case everything
$string = strtolower($string);
//Make alphanumeric (removes all other characters)
$string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
//Clean up multiple dashes or whitespaces
$string = preg_replace("/[\s-]+/", " ", $string);
//Convert whitespaces and underscore to dash
$string = preg_replace("/[\s_]/", "-", $string);
return $string;
}
}
/* End of file blog.php */
/* Location: ./application/controllers/blog.php */
thats it, now create yourself model that can validate ID, grab title...
sample code (of model):
public function isInDb($table, $where = array())
{
$q = $this->db->get_where($table, $where);
return ($q->num_rows() > 0) ? true : false; //returns either true or false, pretty straight forward
}
public function getColumn(){}
Now you use (generate) url www.example.com/1 (this will redirect with 301 header to /1/title) or you can use (generate) www.example.com/1/title link, however if you generate url like: /1/fake-title (title is invalid for id 1 it will not redirect to the correct one)
This solution is SEO friendly.

Laravel Sentry restrict Edit permissions to Creator

I am using Laravel to build a simple Movie management System.
When a User creates a Movie in my DB, I use the following
public function store()
{
$input = Input::except('_token');
$id = Helpers::loggedInUser()->id;
$input['creator_id'] = $id;
$this->title->create($input);
return Redirect::back()->withSuccess( trans('main.created successfully') );
}
This successfully passes the users id and stores in it a creator_id field
I want to restrict users from editing Movies which they did not create. So in the edit function I have
public function edit($title)
{
$title = $this->title->byURi( e($title) );
$id = Helpers::loggedInUser()->id;
$titleuser=$title['creator_id'];
if ( $titleuser = $id )
{
return View::make('Titles.Edit')->withTitle($title)->withType('movies');
}
}
However, this does not seem to work. Anyone with a movie.edit permission in my sentry user db can still see the view.
If you compare two variables you have to use two equal signs, otherwise you set the first variable to the value of the second.
if ( $titleuser == $id )

codeigniter datamapper relationship validation issues

I need to set up the validation rules to validate the related items on a specific object, ie: A user can have no more than 3 products related to it.
I believe DataMapper can check for this validation using _related_max_size rule, but I can't figure out how to use it on the $validation array in the model.
So far I've tried this in both my user and product models:
var $validation = array(
'product' => array(
'rules' => array('max_size' => 3)
)
);
Can somebody show me an example on how to set up this at the model, controller and finally the view?
Edit: What I mean is, a user has many products, and can create a certain amount of them, let's say 3 products, when that amount is reached, the user can no longer create products, and this validation rule should not permit the user to create more products.
This would be the DB Schema:
Users table
------------------
id | username |
------------------
Products table
------------------------
id | user_id | name |
------------------------
More info here: http://codeigniter.com/forums/viewthread/178045/P500/
Thanks!
EDIT:
Ok, I got it all working now… Except, I need to do the following:
var $validation = array(
'product' => array(
'label' => 'productos',
'rules' => array('required','max_size' => $products_limit)
)
);
The $products_limit comes from the “plan” the user has associated, and it’s stored in the session when the user logs in. When I try to run this I get:
Parse error: syntax error, unexpected T_VARIABLE in /var/www/stocker/application/models/user.php on line 11
Is there any way to make this setting dynamic?
In model
var $validation = array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => array('required')
)
);
In controller. $this -> $object = new Your_model();
$object->validate();
if ($object->valid)
{ $object->save();
// Validation Passed
}
else
{ $data['error'] = $object->error;
// Validation Failed
}
In view.
echo $error->field_name
I never use Codeigniter before, but give me a chance to help you. So far I didn't found any built-in validation in Code-igniter (correct me if I'm wrong).
One workaround that I could think of is to Callback:Your own Validation Functions. Below is a snip. Pardon me if it didn't work as you want.
In Model: (create something like)
function product_limit($id)
{
$this->db->where('product_id',$id);
$query = $this->db->get('products');
if ($query->num_rows() > 3){
return true;
}
else{
return false;
}
}
In controller: (create something like)
function productkey_limit($id)
{
$this->product_model->product_exists($id);
}
public function index()
{
$this->form_validation->set_rules('username', 'Username', 'callback_product_limit');
}
For more information Please refer to the manual page which gives more complete. I am also new to CodeIgniter. But I hope this helps you, not complicate you.
First, set up a custom validation rule in libraries/MY_Form_validation.php
If the file doesn't exist, create it.
Contents of MY_Form_validation.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation
{
function __construct($config = array())
{
parent::__construct($config);
}
function valid_num_products()
{
//Perhaps it would be better to store a maxProducts column in your users table. That way, every user can have a different max products? (just a thought). For now, let's be static.
$maxProducts = 3;
//The $this object is not available in libraries, you must request an instance of CI then, $this will be known as $CI...Yes the ampersand is correct, you want it by reference because it's huge.
$CI =& get_instance();
//Assumptions: You have stored logged in user details in the global data array & You have installed DataMapper + Set up your Product and User models.
$p = new Product();
$count = $p->where('user_id', $CI->data['user']['id'])->count();
if($count>=$maxProducts) return false;
else return true;
}
}
Next, set up your rule in config/form_validation.php.
Contents of form_validation.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config = array
(
'addProduct' => array
(
array
(
'field' => 'name',
'label' => 'Product Name',
'rules' => 'required|valid_num_products'
)
)
);
Next, set up your error message in language/english/form_validation_lang.php. Add the following line:
$lang['valid_num_products'] = "Sorry, you have exceeded your maximum number of allowable products.";
Now in the Controller, you'll want something along the lines of:
class Products extends MY_In_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
}
function add()
{
$p = $this->input->post();
//was there even a post to the server?
if($p){
//yes there was a post to the server. run form validation.
if($this->form_validation->run('addProduct')){
//it's safe to add. grab the user, create the product and save the relationship.
$u = new User($this->data['user']['id']);
$x = new Product();
$x->name = $p['name'];
$x->save($u);
}
else{
//there was an error. should print the error message we wrote above.
echo validation_errors();
}
}
}
}
Finally, you might wonder why I've inherited from MY_In_Controller. There is an excellent article written by Phil Sturgeon over on his blog entitled Keeping It Dry. In the post he explains how to write controllers that inherit from access-controlling Controllers. By using this paradigm, controllers that inherit from MY_In_Controller can be assumed to be logged in, and the $this->data['user']['id'] stuff is therefore assumed to be available. In fact, $this->data['user']['id'] is SET in MY_In_Controller. This helps you seperate your logic in such a way that you're not checking for logged in status in the constructors of your controllers, or (even worse) in the functions of them.

Resources