URL not accepting alpha numeric parameter - Yii2-app-basic - php

As soon, i'm passing 41 in URL. confirm.php prints 41.
http://localhost/yii2-app-basic/web/site/confirm/41
But, when i pass "cfeb70c4c627167ee56d6e09b591a3ee" or "41a" in URL,
http://localhost/yii2-app-basic/web/site/confirm/41a
it shows error
NOT FOUND (#404)
Page not found.
The above error occurred while the Web server was processing your request. Please contact us if you think
this is a server error. Thank you.
I want to send confirmation id to user to confirm their account.
That's why random number "cfeb70c4c627167ee56d6e09b591a3ee" is being passed.
So, what can i do to make URL accept alpha numeric parameter.
config/web.php
'urlManager' => [
'showScriptName' => false,
'enablePrettyUrl' => true,
'enableStrictParsing' => false,
'rules' => [
'<controller>/<action>/<id:\d+>' => '<controller>/<action>'
],
],
SiteController.php
public function actionConfirm($id)
{
$id = Yii::$app->request->get('id');
$this->view->params['customParam'] = $id;
return $this->render("confirm",array("id"=>$id));
}

Change this line
'<controller>/<action>/<id:\d+>' => '<controller>/<action>'
to this
'<controller>/<action>/<id:[a-z0-9]+>' => '<controller>/<action>'
That should do it

The current rule states that id is a number(\d+) hence it not working in your examples. Instead of modifying the current rule, I would add one specifically for this case:
'rules' => [
'site/confirm/<id:\w+>' => 'site/confirm',
'<controller>/<action>/<id:\d+>' => '<controller>/<action>'
],

Related

How to use YiiConditionalValidator to work on client side?

I'm using YiiConditionalValidator.php extension to Yii 1.1.20.
I want to have required field (master_id) when i switch button (is_master) from 1 to 0 ...
is_master - 1 or 0
master_id - if "is_master" = 0 make "master_id required...
So... my model rule looks like this:
public function rules()
{
return [
['is_master', 'required'],
['is_master', 'validators.YiiConditionalValidator',
'if' => [
['is_master', 'compare', 'compareValue'=> "0"],
],
'then' => [
['master_id', 'required'],
],
],
['is_master, master_id', 'safe', 'on' => 'search'],
];
}
And in my form i have this options set:
'enableAjaxValidation' => false,
'enableClientValidation' => true,
'clientOptions' => array(
'validateOnChange' => true,
'validateOnSubmit' => true,
),
It seems like my $form cant see this conditional rule...
Thanks for any Help!
While no one knows ^____^ ... I made little research and the answer is that in YiiConditionalValidator.php one function is missing...
When you look at yii 1.1.x framework/validators most of them have 2 functions:
protected function validateAttribute($object,$attribute)
and
public function clientValidateAttribute($object,$attribute)
And that's why YiiConditionalValidator.php isn't working on client side because it lacks public function clientValidateAttribute($object,$attribute) which "Returns the JavaScript needed for performing client-side validation"...
If you want to know how does it look like check your project folder/framework/validators
Best Regards!
Tom

How to find a record using a field other than id in Yii 2

I have the following code where I want to find a model using a field called link instead of id. However, it doesn't seem to produce any results. Where could I be getting it wrong? It returns 404
public function actionView($link)
{
$model = News::find()->where(['link'=>$link])->all();
return $this->render('view', [
'model' => $model,
]);
}
NB: in the search model, I have tried adding this:
$query->andFilterWhere([
'id' => $this->id,
'link'=>$this->link,
'category' => $this->category,
'date' => $this->date,
'userid' => $this->userid,
'featured' => $this->featured,
]);
If you want a single model you need one() and not all()
public function actionView($link)
{
$model = News::find()->where(['link'=>$link])->one();
return $this->render('view', [
'model' => $model,
]);
}
With one() method you retrive just a model and in your $model you have the data you need ..
If you sue all() lie you did you retrive a collection of models and for accessing a single model you must set a proper index eg:
$my_model = $model[0];
In UrlManager config you propably have:
'<controller>/<action>/<id:\d+>' => '<controller>/<action>',
It means default rewrite rules use id as param and it has to be digit, so you can't use controller/view/link. Just to be sure, change action name from actionView to actionTest and then call URL controller/test?link=linklink.
Other solution is to use URL like this: controller/view?link=linklink
The problem is in your url
case 1)
http://localhost/projectName/backend/web/controllerName/actionName/username/rushil
Gives output : Not Found (#404)
case 2)
http://localhost/projectName/backend/web/controllerName/actionName/rushil
Gives output : Not Found (#404)
case 3)
http://localhost/projectName/backend/web/controllerName/actionName?username=rushil
this will work.
Solution : check your url and pass link parameter in url as shown in case 3
404 means your action was not found by url manager. link parameter must be in the url (http://yourapp/controller/view?link=something) because you definded it as actionView($link)
Requesting http://yourapp/controller/view will give you 404 error.
For anyone who may encounter such an error, the problem is after using pretty urls, you need to add rules to map the url format.
C:\xampp\htdocs\your_project\frontend\config\main.php under urlManager add the rules like so:
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
'<controller:\w+>/<id:\d+>' => 'news/view',
'<controller:\w+>/<link>' => 'news/latestnews', // This is required for $link parameter
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>/<link>' => 'news/latestnews',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
],

How to access defined URL in Yii2

I have controller UserController and action actionAjax. I access by Chrome with below URL and it is working normally.
index.php?r=user/ajax
Now I define new action with named actionAjaxUser, still using Chrome to access URL index.php?user/ajaxUser
Then 404 returned.
What should I do to getting content of actionAjaxUser?
add for each additional uppercase Letter after "action" a "-" so in your case
index.php?r=user/ajax-user
So if you would have an action like actionTest1Test2Test3 the url would be controllername/test1-test2-test3.
Be aware that if you are using access rules you also have to use the "url" path.
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['test1-test2-test3'],
'allow' => true,
'roles' => ['#'],
],
],
],
];
The same Naming scheme is btw. also used for Controllers. E.g. if your Controller is named Test1Test2Controller the view folder name would be test1-test2.
Hope this clarifies this for you.

Yii routing - how to put that controller

I need to perform routing for the following url schemas:
website.com/some-category-name
website.com/some-category-name/entryName
some-category-name will be variable - some name of category
How to configure routing for this? I need to enter previous controllers, for example:
website.com/account
website.com/regiter
and want to everything that does not have controller name (so will be category name) going to controller Category.
I can't work it out.
use
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'categoryName/<categoryName:\w+>' => array('site/category'),
'register' => array('site/register'),
'account' => array('site/account')
),
),
At first you must declare all rules for "non Category" actions, and after that dynamic rules (associated with category and antry):
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
// for example if your account and register actions in user controller
// ... you can write
'account' => 'user/account',
'register' => 'user/register',
// or with one rule
'<action(account|register)>' => 'user/<action>',
// and for all other 'static actions', such as login, logout ...
// after yhat you can declire dynamic rules
'<categoryName:\w+>' => 'category/index',
'<categoryName:\w+>/<entryName:\w+>' => 'category/entry'
),
),
So the code Yii::app()->createUrl('user/register') will generate url website.com/register, and accordingly the url website.com/register "goes to" register action of user controller (all other static rules like this way).
Now dynamic rules: code
Yii::app()->createUrl('category/index', array(
'categoryName' => 'first-category-name'
))
will generate url website.com/first-category-name, and vice versa: the url website.com/first-category-name "goes to" category/index action and in it will be available $_GET['categoryName'] parameter, which will be equal to "second-category-name"․
Accordingly code
Yii::app()->createUrl('category/index', array(
'categoryName' => 'some-category-name',
'entryName' => 'some-entry-name'
))
will generate url website.com/some-category-name/some-entry-name, and in category/entry action you can get $_GET['categoryName'] equal to "some-category-name" and $_GET['entryName'] equal to some-entry-name.
I hope this help you to understand how works rules in Yii.
Thanks!

Problem with cakephp validation

I have field in my model whose validation rules is shown below.
'message' => array(
'rule' => '/^[a-z0-9#.,&; ]{2,255}$/i',
'required' => true,
'allowEmpty' => false,
'message' => '(message field contains only a-z0-9#.,&; and is between 2 to 255 characters.).'
),
This works fine. When I add forwardslash(/) to the rule '/^[a-z0-9#.,&;/ ]{2,255}$/i' or '/^[a-z0-9#.,&;\/ ]{2,255}$/i'. then it does not work.
I appreciate any help.
Thanks.
That isn't a backslash, it is a forward slash.
So I don't know which slash you want to use, but one of these should work:
'/^[a-z0-9#.,&;\\ ]{2,255}$/i'
or
'/^[a-z0-9#.,&;\/ ]{2,255}$/i'

Categories