yii2 url not found shown while accessing controller action - yii2

I have created a controller function like
public function actionRateTicket($id){
}
The urlManager has the following configuration
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,'enableStrictParsing' => true,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
But when I tried to access http://mydomainname/web/ticket/rate-ticket/2333 , (ticket is the name of controller(TicketController.php)) it is showing Not Found (#404) error.
What is the problem here?
I am able to access all controller actions with a single camel case characters like actionView, actionEdit etc, but actionRateTicket, I am not able to acess. If the actionRateTicket is renamed to actionRate, it is working.
My controller is like this
<?php
namespace app\controllers;
use Yii;
use app\models\Techs;
use app\models\Ticket;
use app\models\Assignment;
use app\models\TicketSearch;
use app\models\ComplainType;
use app\models\TicketComplain;
use app\models\User;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\models\Adusers;
use yii\web\UploadedFile;
use app\helpers\Utils;
use yii\db\Expression;
use yii\filters\AccessControl;
/**
* TicketController implements the CRUD actions for Ticket model.
*/
class TicketController extends Controller {
public function behaviors() {
return [
'access' => [
'class' => AccessControl::className(),
'only' => [
'index',
'view',
'update',
'delete',
'newticket'
],
'rules' => [
[
'actions' => [
'index',
'view',
'update',
'delete',
'newticket'
],
'allow' => true,
'roles' => ['#'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
public function actionRateTicket($id) {
echo "in"; echo $id;
exit;
}
}
?>
My .htaccess of web folder is
RewriteEngine on
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php

Change the action name for actionRateticket, and the call should be http://mydomainname/web/ticket/rateticket/2333 with rateticket.

try using the following line:
use yii\helpers\Url;

I believe it is because of the weird way that Yii uses dashes instead of camel case. Try changing your rules from <action:\w+> to <action:[-\w]+> since the dash is not a word character!
I can't find the default rules to verify that they also check for the dash but that should work.

you need to specify the action in access behavior
'access' => [
'class' => AccessControl::className(),
'only' => ['rate-ticket', 'index','view','update','delete','newticket' ],
'rules' => [
[
'actions' => ['rate-ticket', 'index','view','update','delete','newticket', ],
'allow' => true,
'roles' => ['#'],
],
],
],

yii2 uses more SEO friendly url for camel case controller. All camel case is converted to lowercase with dash like this:
UserProfileController
index.php?r=user-profile

Related

Yii2 hide action name from url

I'm trying to remove action name from url. 'post/view' to 'page' using urlManager but it's not working
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Hide index.php
'showScriptName' => false,
// Use pretty URLs
'enablePrettyUrl' => true,
'rules' => [
//'<view:\w+>' => 'post/<alias>',
//'<alias:view>' => 'post/<alias>',
'page' => 'post/view',
],
],
Change your config to include the id:
'page/<id>' => 'post/view'
Allows you to use urls like this:
localhost:8585/yii2basic/wfp/web/page/PGr1mtIkAE
You can't really do this:
localhost:8585/yii2basic/wfp/web/post/id=PGr1mtIkAE

Yii2 enablePrettyUrl fail and frontend post request fail

i was trying to make
this url
http://localhost/advanced/frontend/web/index.php?r=test
into
htttp://localhost/advanced/frontend/web/tests in yii2
also to receive post request
this in my codding in common/config/main.php
<?php
return [
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'urlManager' => [
],
'urlManagerFrontEnd' => [
'enablePrettyUrl' => true,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => ['test'],
'only' => ['index', 'create'],
],
],
'showScriptName' => false,
],
],
];
this in my frontend testController
namespace frontend\controllers;
use Yii;
use yii\rest\ActiveController;
use common\models\Test;
use yii\data\ActiveDataProvider;
class TestController extends ActiveController {
public $modelClass = 'common\models\Test';
public function actions(){
$actions = parent::actions();
unset($actions['create']);
return $actions;
}
public function actionCreate(){
$model = new Test();
$model->load(Yii::$app->request->post(),'');
$model->save();
return $model;
}
}
i have a problem in the post request when i tried
Postman result jpg
so i have 2 problem the first is the prettyUrl and the post request
if u have a solution please make it easy because i just learn php yii2 html css etc for 1 month

What is function behaviors in SiteController.php?

I am starting up with yii2
I dont have any idea of previous version of YII
But I have good knowledge of codeigniter and have been working in codeigniter from last 3 years.
MY Question is as below :
There is a function name behaviors() in SiteController.php file.
It has below code. I want to know what does it do?
public function behaviors() {
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout'],
'rules' => [
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['#'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
In the sample you privided there are two part
'access'
and
'verbs'
the access section configure the Access Control Filter rules
http://www.yiiframework.com/doc-2.0/yii-filters-accesscontrol.html
one of the authorization method implemented by Yii2 ( best used by applications that only need some simple access control)
and http://www.yiiframework.com/doc-2.0/yii-filters-verbfilter.html
that define the allowed HTTP request methods for each action. VerbFilter checks if the HTTP request methods are allowed by the requested actions. If not allowed, it will throw an HTTP 405 exception.
In your case set that the action logout must performed by a post method
for a brief guide you can see
http://www.yiiframework.com/doc-2.0/guide-structure-filters.html

How to create a pretty URL?

How can I create a pretty url like this
customer/index/amount/12000/location/in . In Yii1.1 it was available by default. it was easy to enable pretty url. In Yii2 if need pretty url I've to write rules for every action !!!
In Codeigniter you will get index.php/controller/action/parameter1/parameter2/parameter3
ie it does not expose action parameter variables, that too without writing any url rules!
===Edit===
pretty URL doesnt work for parameters if no rule is defined
below is my main.php
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [ '<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],
i am getting a lot of 404's, like
192.168.1.3/~user/urshow/frontend/web/movies/movies_all
it would have work fine if it would be like this
192.168.1.3/~user/urshow/frontend/web/index.php?r=/movies/movies_all
Go to config.php and add
'components' => [
...
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
],
...
],

Yii 2 static pages

I can't show static pages. Try do it as described in doc here - http://stuff.cebe.cc/yii2-guide.pdf (on page 100) but when I enable prettyurl, it doesn't work.
Added in urlManager rules:
'urlManager' => array(
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => array(
'' => 'site/index',
'login' => 'site/login',
'contacts' => 'site/contact',
'<view:(break)>'=>'/site/page?&view=<view>',
),
),
then in SiteController added:
public function actions()
{
return [
...
'page' => [
'class'=>'yii\web\ViewAction',
],
];
}
And then created views/site/pages/break.php
<h1>View static page Break</h1>
But I get an error:
Not Found (#404)
Unable to resolve the request: site/page?&view=break
If i disable prettyUrl:
//'enablePrettyUrl'=>true
then i can see my page typing url: index.php?r=site/page&view=break
What's wrong with ViewAction?
I think you are doing the rules part of your url manage wrong.
Try this
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
The rules part should be important..
I solved my problem. use such lines:
'<view:(break)>' => 'site/page',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
I force use specific name of page for view, in my case it "break", because can't use this
'<view:[a-zA-Z0-9-]+>' => 'site/page',
(it causes crashing other rules.) I think it could better create "own rule class" extending UrlRule, but think that now I don't need this.
I had tried this way (without rules specification) :
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => 'false'
],
Solution is simple:
web.php code is like this 'rules' => [
'site/page/<view:[a-zA-Z0-9-]+>' => 'site/index',
In SiteController don't use function actions(), instead:
public function actionIndex ($view)
{
return $this->render('/site/pages/' . $view);
} catch (InvalidParamException $e) {
throw new HttpException(404);
}.
If view contacts.php exists in views/site/pages/ , url is yourdomain/basic/web/site/page/contact
4.Thanks to samdark and it's article https://github.com/yiisoft/yii2/issues/2932