rest yii2 pretty url urlManager - yii2

My yii2 rest work fine with this request
http://extractor-frontend.dev/property?id=JP000004
i would to work with this
http://extractor-frontend.dev/property/JP000004
this is my urlManager in config/web.php
urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class'=>'yii\rest\UrlRule',
'pluralize' => false,
'controller' => 'property',
'tokens' => [
'{id}' => '<id:\\w+>'
],
'extraPatterns' => ['GET,HEAD property/{id}' => 'index',]
]
],
],
this is my .htaccess in web
RewriteEngine on
Options Indexes
Require all granted
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php
if put 'enableStrictParsing' => false,
http://extractor-frontend.dev/site/about
work fine ... rewrite rules works!

I use below code for my yii2 apps. I think your config method is eligible for yii1. It is recommended to use yii2 config method.
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'myaction/<id>/<param2>' => 'site/myaction',
[
'pattern' => '<action>',
'route' => 'site/<action>',
'defaults' => ['action' => 'index']
],
]
]

Related

Yii2 Backend show 500 internal server error

When I access http://alpha.myweb.com/administrator/company I got 500 Internal Server Error
But I can access it localhost.
How I can debug my problem?
Here is my backend\config\main.php
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'id' => 'app-backend',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'bootstrap' => ['log'],
'modules' => [
'agent' => [
'class' => 'backend\modules\agent\Agent',
],
'company' => [
'class' => 'backend\modules\company\Company',
'defaultRoute' => 'home',
],
'vendor' => [
'class' => 'backend\modules\vendor\Vendor',
'defaultRoute' => 'default',
],
'services' => [
'class' => 'backend\modules\services\Services',
'defaultRoute' => 'site',
],
],
'homeUrl' => 'http://alpha.myweb.com/administrator',
'components' => [
'formatter' => [
'class' => 'yii\i18n\Formatter',
'thousandSeparator' => '.',
'decimalSeparator' => ',',
//'currencyCode' => '$'
],
'user' => [
'identityClass' => 'common\models\UserBeo',
'enableAutoLogin' => false,
//'authTimeout' => 120
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'showScriptName' => false,
/*
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
*/
],
'request' => [
'baseUrl' => '/administrator',
'parsers' => [
'application/json' => 'yii\web\JsonParser',
]
],
/*
'response' => [
'format' => yii\web\Response::FORMAT_JSON,
'charset' => 'UTF-8',
// ...
]
*/
],
'params' => $params,
];
Here is my .htaccess
# prevent directory listings
Options -Indexes
IndexIgnore */*
# follow symbolic links
Options FollowSymlinks
RewriteEngine on
RewriteRule ^administrator(/.+)?$ backend/web/$1 [L,PT]
RewriteRule ^(.+)?$ frontend/web/$1
#RewriteCond %{HTTP_HOST} ^alpha\.myweb\.com [NC]
#RewriteRule ^(.*)$ http://alpha.myweb.com/$1 [L,R=301]
So What cause this problem, because I can open frontend website without error.
Please let me know if you need more information.
Thanks in advance, any help appreciated.

yii2 disable access by right part of routing rule

In web.php I have following code:
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
'calc' => 'site/calc',
),
],
And I want to allow users to access to example.com/calc but not to example.com/site/calc. How I can do it properly with less effort? By other words now works both options - "site/calc" and "calc" and I want to disable "site/calc".
Try to add enableStrictParsing to UrlManager configuration as follows
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'rules' => array(
'calc' => 'site/calc',
),
],
In this case if there's no rule matching the request then it's considered as bad one.

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,
],
...
],

How to config yii2 urlManager rules?

Here is Yii2 code in main.php:
'urlManager' => [
'baseUrl' => $baseUrl,
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'suffix' => '.html',
'rules' => [
// site controller
'' => 'site/index',
'contact_us' => 'site/contact',
// sitemap controller
'sitemap' => 'sitemap/index'
]
And url in browser is:
+ Site controller:
- http://localhost/neko/
- http://localhost/neko/contact_us.html
+ Sitemap controller:
- http://localhost/neko/sitemap/index.html
How to I configure my sitemap controller to http://localhost/neko/sitemap.xml?
Use array configuration for sitemap.xml, like that:
'rules' => [
// site controller
'' => 'site/index',
'contact_us' => 'site/contact',
// sitemap controller
[
'pattern' => 'sitemap',
'route' => 'sitemap/index',
'suffix' => '.xml',
],
],
See docs.

yii2 url not found shown while accessing controller action

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