How to use beforeAction in Yii or slug troubles - php

Yii-jedis!
I'm working on some old Yii-project and must to add to them some features. Yii is quite logical framework but it has some things I couldn't understand. Perhaps I haven't understand Yii-way yet. So I'll describe my problem step-by-step. For impatients - briefly question at the end.
Intro: I want to add human-readable URLs to my project.
Now URLs looks like: www.site.com/article/359
And I want them to look like this: www.site.com/article/how-to-make-pretty-urls
Very important: old articles must be available on old format URLs, and new - on new URLs.
Step 1: First, I've updated rewrite rules in config/main.php:
'<controller:\w+>/<id:\S+>' => '<controller>/view',
And I've added new texturl column to article table. So we will store here human-readable-part-of-url for new articles. Then I've updated one article with texturl for tests.
Step 2: Application show articles in actionView of ArticleController so I've added there this code for preproccessing ID parameter:
if (is_numeric($id)) {
// User try to get /article/359
$model = $this->loadModel($id); // Article::model()->findByPk($id);
if ($model->text_url !== null) {
// If article with ID=359 have text url -> redirect to /article/text-url
$this->redirect(array('view', 'id' => $model->text_url), true, 301);
}
} else {
// User try to get /article/text-url
$model = Article::model()->findByAttributes(array('text_url' => $id));
$id = ($model !== null) ? $model->id : null ;
}
And then begin legacy code:
$model = $this->loadModel($id); // Load article by numeric ID
// etc
It works perfectly! But...
Step 3: But we have many actions with ID parameter! What we have to do? Update all actions with that code? I think it's ugly. I've found CController::beforeAction method. Looks good! So I declare beforeAction and place ID preproccessing there:
protected function beforeAction($action) {
$actionToRun = $action->getId();
$id = Yii::app()->getRequest()->getQuery('id');
if (is_numeric($id)) {
$model = $this->loadModel($id);
if ($model->text_url !== null) {
$this->redirect(array('view', 'id' => $model->text_url), true, 301);
}
} else {
$model = Article::model()->findByAttributes(array('text_url' => $id));
$id = ($model !== null) ? $model->id : null ;
}
return parent::beforeAction($action->runWithParams(array('id' => $id)));
}
Yes, it works with both URL-formats, but it executes actionView TWICE and shows page two times! What can I do with this? I've totally confused. Have I choose a right way to solve my problem?
Briefly: Can I proceess ID (GET-parameter) before execute of any actions and then run requested action (once!) with modified only ID parameter?

Last line should be:
return parent::beforeAction($action);
Also to ask you i didnt get your step:3.
As you said you have many controller and you don't need to write code in each file, so you are using beforeAction:
But you have only text_url related to article for all controllers??
$model = Article::model()->findByAttributes(array('text_url' => $id));
===== updated answer ======
I have changed this function, check now.
If $id is not nummeric then we will find it's id using model and set $_GET['id'], so in further controller it will use that numberic id.
protected function beforeAction($action) {
$id = Yii::app()->getRequest()->getQuery('id');
if(!is_numeric($id)) // $id = how-to-make-pretty-urls
{
$model = Article::model()->findByAttributes(array('text_url' => $id));
$_GET['id'] = $model->id ;
}
return parent::beforeAction($action);
}

Sorry, I haven't read it all carefully but have you considered using this extension?

Related

Zend Framework 2 - How to keep value in form fields ? on multiple record update?

I am working on zend framwork 2
I have created one module with two fields
1) Test1
2) Test2
Database structure for this :
db name : zend_test_db
db fields : config_key , config_value
I want to store like config_key = test1key and config_value : textbox
enter value
Multiple records store at a time.
below is my controller function :
public function indexAction()
{
$form = new ConfigurationForm();
$form->get('submit')->setValue('Save Settings');
$form->get('test1key')->setValue('test1key');
$form->get('test2key')->setValue('test2key');
$request = $this->getRequest();
if ($request->isPost()) {
$configuration = new Configuration();
$form->setInputFilter($configuration->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$configuration->exchangeArray($form->getData());
$this->getConfigurationTable()->saveConfiguration($configuration);
// Redirect to list of configuration
return $this->redirect()->toRoute('configuration');
}
}
return array('form' => $form);
}
Above code works fine on Add fields . I am able to insert those fields and stored as key and value
But i am not able to update this.
Hope its clear
Where do i make mistake ?
I am not able to comment as I have reputation less then 50. I think you are trying to say that you are able to insert the data in database but you are not able to update it.
This is happening because you are creating new model every time.
$configuration = new Configuration();
You should initialize it using id params.
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('configuration', array(
'action' => 'add'
));
}
try {
$configuration = $this->getConfigurationTable()->getConfiguration($id);
}
catch (\Exception $ex) {
return $this->redirect()->toRoute('configuration', array(
'action' => 'index'
));
}
Using this you will be able to update the data as well. For more reference you can check zend framework Album module. Here is the link
https://framework.zend.com/manual/2.2/en/user-guide/forms-and-actions.html
If this was not the problem then please let me know so that I can help you in this concern.

Creating yii2 dynamic pages with url: www.example.com/pageName

In my system, users need to have their profile pages. It is requested from me that these pages will be displayed in url like this:
www.example.com/John-Doe
www.example.com/Mary-Smith
How to achieve these URLs in yii2 ? These John-Doe and Mary-Smith can be user usernames or profile names. For example I have field in user table called "name" and it will hold names "John Doe", "Mary Smith". Pay attention that I need SEO friendly URLs with "-" instead of blank spaces.
URLs like this:
www.example.com/profile/view?id=1
are not an option.
www.example.com/John-Doe
www.example.com/Mary-Smith
I think there is no normal way to use these urls because at first controller (in your case it's ProfileController) needs to be determined. From these urls it's impossible to do.
Second problem with the urls you provided - uniqueness is not guaranteed. What if another user with name John Doe will sign up on site?
Look for example at your profile link at Stack Overflow:
http://stackoverflow.com/users/4395794/black-room-boy
It's not http://stackoverflow.com/black-room-boy and not even http://stackoverflow.com/users/black-room-boy.
Combining id and name is more widespread and robust approach. Also they can be combined with dash like this: http://stackoverflow.com/users/4395794-black-room-boy
Yii 2 has built-in behavior for this, it's called SluggableBehavior.
Attach it to your model:
use yii\behaviors\SluggableBehavior;
public function behaviors()
{
return [
[
'class' => SluggableBehavior::className(),
'attribute' => 'name',
// In case of attribute that contains slug has different name
// 'slugAttribute' => 'alias',
],
];
}
For your specific url format you can also specify $value:
'value' => function ($event) {
return str_replace(' ', '-', $this->name);
}
This is just an example of generating custom slug. Correct it according to your name attribute features and validation / filtering before save.
Another way of achieving unique url is setting $ensureUnique property to true.
So in case of John-Doe existense John-Doe-1 slug will be generated and so on.
Note that you can also specify your own unique generator by setting $uniqueSlugGenerator callable.
Personally I don't like this approach.
If you choose the option similar to what Stack Overflow uses, then add this to your url rules:
'profile/<id:\d+>/<slug:[-a-zA-Z]+>' => 'profile/view',
In ProfileController:
public function actionView($id, $slug)
{
$model = $this->findModel($id, $slug);
...
}
protected function findModel($id, $slug)
{
if (($model = User::findOne(['id' => $id, 'name' => $slug]) !== null) {
return $model;
} else {
throw new NotFoundHttpException('User was not found.');
}
}
But actually id is enough to find user. Stack Overflow does redirect if you access with correct id but different slug. The redirects occurs when you are completely skipping the name too.
For example http://stackoverflow.com/users/4395794/black-room-bo redirects to original page http://stackoverflow.com/users/4395794/black-room-boy to avoid content duplicates that are undesirable for SEO.
If you want use this as well, modify findModel() method like so:
protected function findModel($id)
{
if (($model = User::findOne($id) !== null) {
return $model;
} else {
throw new NotFoundHttpException('User was not found.');
}
}
And actionView() like so:
public function actionView($id, $slug = null)
{
$model = $this->findModel($id);
if ($slug != $model->slug) {
return $this->redirect(['profile/view', ['id' => $id, 'slug' => $model->slug]]);
}
}

Modify a row with CakePHP

I'm using CakePHP to show a frontend GUI for a MySQL database table. I've used bake to auto generate the screens and I currently have a fully functioning app with View, Edit and Delete buttons per row. I want to add a button per row, called Accept, which should set IsAccepted = 1 on the SQL row.
I've managed to add an Accept button per row as follows:
echo $this->Html->link(__('Accept'), array('action' => 'accept', $product['Product']['ID']))
But the code in ProductController.php does not work:
public function accept($id = null){
...
$this->Product->IsAccepted = 1; // does not work, silently fails
}
What am I doing wrong? How do I properly edit a row using a per-row button?
public function accept($id = null){
$this->Product->save(array('id' => $id, 'is_accepted' => 1));
}
// assuming cake 2.1+
public function accept($id = null){
if($this->Product->exists($id)) {
$this->Product->saveField('is_accepted', 1);
// success..
}
// else throw not found exception...
}
Thanks to cornelb I found the answer! This is the final code I used to modify a row, with a per-row button.
Modifies the row when the per-row button is pressed (works just like a POST/AJAX button)
A flash message that says "Accepted!" shows if saving succeeds
Redirects back to the listing page (appears to never leave the listing)
This is the code that goes in ProductController.php (or whatever controller class you have):
public function accept($id = null) {
if ($this->Product->exists($id)) {
// save the row
// you absolutely need to fill the 'id' slot, even if its not your primary key!!
// this ensures that the row is EDITED, and not INSERTED!
if($this->Product->save(array('id' => $id, 'ID' => $id, 'IsApproved' => 1, 'ApprovedDate' => date('Y-m-d H:i:s', time())))){
// show a "flash" message
// (not Adobe Flash, just a message that shows on top of the list)
$this->Session->setFlash(__('The product has been accepted!'));
// this action does not have a view so no need to render
$this->autoRender = false;
// redirect to index view
return $this->redirect(array('action' => 'index'));
}
}
}
**Try this.....**
<?php
public function accept($id = null) {
$this->autoRender = false; // if action has not view.
if ($this->Product->exists($id)) {
$this->Product->id = $id;
if ($this->Product->save(array('is_accepted' => 1))) {
$this->Session->setFlash(__('The product has been accepted!'));
return $this->redirect(array('action' => 'index'));
}
}
}
?>
Just run updateAll query from accept function as shown below:
public function accept($id = null){
if(!empty($id)){
$this->Product->updateAll(
array('Product.is_accepted' => 1),
array('Product.id' => $id)
);
}
}
Hope this will help you...
For reference: http://book.cakephp.org/2.0/en/models/saving-your-data.html

is_unique for codeigniter form validation

I'm trying to figure out how I can use the is_unique rule from the Codeigniter form validation library in the following situation.
I'm trying to submit a edit user form and have the rule:
$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean|is_unique[users.user_name]');
What if other values in the form are being changed but this value stays the same. The form is going to see that this value already exists so how would I protect it from editing if this value isn't changed.
Using your code as an example, the is_unique validation rule works by looking for a field called user_name in your users database table. If the field with the same value exists it validates as false.
To make sure it runs only when the user submits a new value, you could check the posted value $this->input->post('user_name') against the value you pulled from the database to populate your form with. If they are the same, don't validate is_unique;
if($this->input->post('user_name') != $original_value) {
$is_unique = '|is_unique[users.user_name]'
} else {
$is_unique = ''
}
$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean'.$is_unique);
There's a better way to go around it, I think, still using CodeIgniters' validation library...
Use edit_unique where you pass an extra parameter which is the id of the row you're editing.. See below.. I use it and works pretty fine for me.. hope it helps
$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean|edit_unique[users.user_name.'.$id.']');
$something = $this->input->post('something');
$this->form->validation->set_rules('something','Something','xss_clean|is_unique['tbl'.users]');
if($this->form_validation->run()== FALSE){
}
Simple Way
Just Change isset to is_object in system/libraries/form_validation.php
public function is_unique($str, $field)
{
sscanf($field, '%[^.].%[^.]', $table, $field);
return is_object($this->CI->db) //default isset
? ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0)
: FALSE;
}
Here's an easy method that worked for me and uses well documented code (thanks to https://github.com/ivantcholakov for sharing it!). I found it referenced at https://github.com/bcit-ci/CodeIgniter/issues/3109#issuecomment-46346280
Download https://github.com/ivantcholakov/starter-public-edition-3/blob/master/platform/application/libraries/MY_Form_validation.php (MIT licensed) and save it to your application at application\libraries\MY_Form_validation.php
Delete these two lines from __construct():
$this->CI->load->helper('checkbox');
$this->CI->load->helper('email');
Delete all the functions except __construct() and unique().
At the end of the __construct() method of your controller add this line:
$this->load->library('form_validation');
As per the documentation of the unique() method update your validation rule to add a "unique" rule like this (e.g. if you already have required and trim rules):
…|required|unique[tablename.fieldname,tablename.(primaryKey-used-for-updates)]|trim...
Extend Form_validation.php library create class inside of application/libraries file name MY_Form_validation.php
<?php
class MY_Form_validation extends CI_Form_validation{
protected $ci;
public function __construct($config = array()){
parent::__construct($config);
$this->ci =& get_instance();
}
public function is_unique_update($str, $field){
$explode=explode('#', $field);
$field_name=$explode['0'];
$field_id_key=$explode['1'];
$field_id_value=$explode['2'];
sscanf($field_name, '%[^.].%[^.]', $table, $field_name);
if(isset($this->ci->db)){
if($this->ci->db->limit(1)->get_where($table, array($field_name => $str,$field_id_key=>$field_id_value))->num_rows() === 0){
$this->ci->form_validation->set_message('is_unique_update', 'The {field} field must contain a unique value.');
return false;
}
return true;
}
}
}
Now in your controller
$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean|is_unique_update[users.user_name#id#'.$id.']');
"#" I used for explode the string
where id is primary key of users table
and $id is the value of id.
Now you can use this is_unique_update validation in any controller.
This question is very old but maybe some new people experience this problem and this is the solution for it.
I bet your are using Modular Extensions (HMVC) and you have created a new library, MY_Form_validation. You did id for callbacks, so you have this line of code on your class in order to use callbacks:
$this->form_validation->CI =& $this;
Well, the solution to this is whenever you want to use "is_unique" you must delete this line of code "$this->form_validation->CI =& $this;" from the class. I have experienced this problem and i fix it this way, it works fine now.
If you realy want to use callbacks "$this->form_validation->CI =& $this;", then do it only on required "methods" / "functions" where you don't want to use is_unique.
This code helpful for unique validation to create and update function...
In controller
Add this form validation code in both create and update function
$this->form_validation->set_rules('order_no', 'Order no', 'required|callback_check_order_no');
Add this call back function in controller
function check_order_no($order_no) {
if($this->input->post('id'))
$id = $this->input->post('id');
else
$id = '';
$result = $this->Data_model->check_unique_order_no($id, $order_no);
if($result == 0)
$response = true;
else {
$this->form_validation->set_message('check_order_no', 'Order no already exist');
$response = false;
}
return $response;
}
In model
function check_unique_order_no($id = '', $order_no) {
$this->db->where('order_no', $order_no);
$this->db->where('status', "A");
if($id) {
$this->db->where_not_in('id', $id);
}
return $this->db->get('delivery_order')->num_rows();
}
I'm using codeigniter3 and it shows me error when I check username on updating the value, is_unique is not designed to work with update scenario
so using #Anthony Mutisya's answer, here is the complete solution
in your controller, add this line while validation username of the current user with the database
$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean|edit_unique[users.user_name.'.$id.']');
You can get that $id from your submited form.
Now, add the following function to /system/libraries/Form_Validation.php this file. System folder is present in your root of CodeIgniter3 folder.
/**
* edit_unique // for check on update value
*
* Check if the input value doesn't already exist
* in the specified database field.
*
* #param string $str
* #param string $field
* #return bool
*/
function edit_unique($value, $params) {
$CI =& get_instance();
$CI->load->database();
$CI->form_validation->set_message('edit_unique', "Sorry, that %s is already being used.");
list($table, $field, $current_id) = explode(".", $params);
$query = $CI->db->select()->from($table)->where($field, $value)->limit(1)->get();
if ($query->row() && $query->row()->id != $current_id)
{
return FALSE;
} else {
return TRUE;
}
}
It works perfectly fine in my case
CodeIgniter 4 has already solution for that,
$validation->setRules([
'email' => 'required|valid_email|is_unique[users.email,id,{id}]',
]);
$_POST = [
'id' => 4,
'email' => 'foo#example.com',
];
then the {id} placeholder would be replaced with the number 4, giving this revised rule:
$validation->setRules([
'email' => 'required|valid_email|is_unique[users.email,id,4]',
]);
Official Documentation
we must have to add table name for is_unique
for Exp.
is_unique[users.email]

Replace ID from CakePHP URL

How can I replace the /posts/view/id with /posts/view/code ?
code is a field in the database that contains 10 random numbers generated using mt_rand() function.
In the PostsController I have this function for viewing posts:
public function view($id = null) {
$this->Post->id = $id;
$this->set('post', $this->Post->read());
}
Now I want to use code instead of id.
Thank you for your answers!
UPDATE:::
Someone from the CakePHP Q&A solved my problem.
I'll put the codes here so if someone needs the same solution, it can be found here.
in your routes.php add this code:
Router::connect('/posts/view/:code', array('controller' => 'news', 'action' => 'view'), array('pass' => array('code')));
in your controller change the view method like this:
public function view($code = null) {
$this->set('post', $this->Post->findByCode($code));
}
Thank you so much!
i used cakePHP
just few times, but i think you should see routing in cakePHP, you should change rewrite of url
I think that would be a URL like /posts/view/id/code. Then using the ID, you grab the code from the database:
public function view($id = null, $code = null) {
$this->Post->id = $id;
//..DO CAKEPHP QUERY TO GET CODE USING ID HERE. ASSIGN TO $code.
$this->Post->code = $code;
$this->set('post', $this->Post->read());
}

Categories