besides default endpoint create custom endpoints - yii

I am following this tutorial
http://learncooltech.com/yii2-angular-how-to-create-single-page-application-in-minutes/
I got this tutorial to work and now I can see GET /books: list all books page by page;
However, now I want to create some endpoints and in my controller I added a method test but I get a 404. here is my controller
namespace app\controllers;
use yii\rest\ActiveController;
class BookController extends ActiveController
{
public $modelClass = 'app\models\Book';
public function behaviors()
{
return
\yii\helpers\ArrayHelper::merge(parent::behaviors(), [
'corsFilter' => [
'class' => \yii\filters\Cors::className(),
],
]);
}
public function actionTest(){
return json_encode(array("name"=>"John Doe"));
}
}
I have this as my urlRule
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => ['book']],
],
],
and I am doing
http://localhost/events/test
to get to the endpoint
and I get this
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-param" content="_csrf">
<meta name="csrf-token" content="cWdHTlZnWE8uNn5jEDQVHwZXJDcUIykKADUjAgM/Ei4dXn4mOAwNCg==">
<title>Not Found (#404)</title>
<link href="/assets/a7503887/css/bootstrap.css" rel="stylesheet">
<link href="/css/site.css" rel="stylesheet">
</head>
<body>

got it working by changing rule to this
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' =>
['book'],
'extraPatterns' => [
'GET test' => 'test',
],
],

Related

Not Found Page Working On Backend But Not Working at Frontend Yii2

I have yii installed on my live server we have two interfaces one backend and one frontend. In backend we add products and in front end the view is rendered for the products. The problem is that for example this is our website https://mmstore.be/products/714/IPhone-12-Mini-Scherm-Reparatie product page if you put anything at the last of the url like this https://mmstore.be/products/714/IPhone-12-Mini-Scherm-Reparatie/03003300303094949949494 the page will remain the same means it should be showing not found page but the same page comes again.
Below is my config file code frontend.
<?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-frontend',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'frontend\controllers',
'components' => [
'request' => [
'csrfParam' => '_csrf-frontend',
],
'cache' => [
'class' => 'yii\caching\FileCache',],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
],
'session' => [
// this is the name of the session cookie used for login on the frontend
'name' => 'advanced-frontend',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'urlManager' => [
'baseUrl' => '',
'enablePrettyUrl' => false,
'showScriptName' => false,
'enableStrictParsing' => true,
'rules'=>array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'class' => 'yii\web\UrlNormalizer',
'collapseSlashes' => true,
'normalizeTrailingSlash' => true,
),
],
],
'params' => $params,
];
i have error.php in my site directory and below is its code
<?php
/* #var $this yii\web\View */
/* #var $name string */
/* #var $message string */
/* #var $exception Exception */
use yii\helpers\Html;
$this->title = $name;
?>
<h1><?= Html::encode($this->title) ?></h1>
<div class="alert alert-danger">
<?= nl2br(Html::encode($message)) ?>
</div>
<p>
The above error occurred while the Web server was processing your request.
</p>
<p>
Please contact us if you think this is a server error. Thank you.
</p>
It is strange that you disabled "enablePrettyUrl", and using pretty url. If "/products/" is a controller name and assuming "IPhone-12-Mini-Scherm-Reparatie" is a parameter named "url" then you can change urlManager like this:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'/'=>'site/index',
'/products/<id>/<url>'=>'products/view',
'/products/<id>'=>'products/view',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],
in controller it should be something like this
public function actionView($id, $url) {
$product = Product::find()->where(['id'=>$id])->one();
// ...
}

Prestashop 1.7 - Bug FormBuilder on Cms Page Category

I wrote a Prestashop module to add two fields in the CMS Category but the render is wrong.
My source code:
<?php
class NewsSlider extends Module {
public function hookActionCmsPageCategoryFormBuilderModifier(array $params) {
$formBuilder = $params['form_builder'];
$locales = $this->get('prestashop.adapter.legacy.context')->getLanguages();
$formBuilder->add($this->name.'_cover_lang',
\PrestaShopBundle\Form\Admin\Type\TranslatableType::class,
[
'type' => \Symfony\Component\Form\Extension\Core\Type\FileType::class,
'label' => $this->l('Image de couverture'),
'options' => [
'required' => false,
'constraints' => [
'mimeTypes' => [
'image/png',
'image/jpeg'
],
'mimeTypesMessage' => 'JPEG/PNG',
]
],
'required' => false,
]
);
$formBuilder->add($this->name.'_header_lang',
\PrestaShopBundle\Form\Admin\Type\TranslateType::class,
[
'type' => \PrestaShopBundle\Form\Admin\Type\FormattedTextareaType::class,
'label' => $this->l('EntĂȘte de la page'),
'locales' => $locales,
'hideTabs' => false,
'required' => false
]
);
$languages = Language::getLanguages(true);
foreach($languages as $lang){
$content = $this->getCMSHeader($params['id'], $lang['id_lang'], $isCategory);
if(is_string($content) && strlen($content)) {
$params['data'][$this->name.'_header_lang'][$lang['id_lang']] = $content;
}
}
$formBuilder->setData($params['data']);
}
}
?>
You can see the render here.
And I wrote exactly the same code for the CMS Page (hookActionCmsPageFormBuilderModifier) and it's working. Why is it different?

Not able to submit form inside footer in Yii2

I've a subscribe newsletter form inside footer that display on all page. To do this I've created a subscriber widget like this:
SubscriberWidget.php
<?php
namespace frontend\components;
use Yii;
use yii\base\Widget;
use yii\helpers\Html;
use frontend\models\SubscribeNewsletterForm;
class SubscriberWidget extends Widget
{
public function run()
{
$subscriber_model = new SubscribeNewsletterForm();
return $this->render('_subscribe-newsletter-form.php', [
'subscriber_model' => $subscriber_model
]);
}
}
?>
Here's the SubscribeNewsletterForm model code:
SubscribeNewsletterForm.php
<?php
namespace frontend\models;
use Yii;
use yii\base\Model;
class SubscribeNewsletterForm extends Model
{
public $email;
public function rules()
{
return [
[['email'], 'required'],
['email', 'email']
];
}
}
?>
Here is the code of my _subscribe-newsletter-form.php
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\helpers\Url;
?>
<h3>Subscribe to Newsletter</h3>
<?php $form = ActiveForm::begin(['id' => $subscriber_model->formName(), 'action' => ['project/subscriber'], 'validateOnBlur' => false, 'validateOnType' => false]); ?>
<div class="input-group">
<?= $form->field($subscriber_model, 'email')->textInput()->label(false); ?>
<span class="input-group-btn">
<?php echo Html::submitButton('Sign Up', ['class' => 'btn btn-primary subscribe-btn']); ?>
</span>
</div>
<?php ActiveForm::end(); ?>
<?php
$script = <<< JS
$('#{$subscriber_model->formName()}').on('beforeSubmit', function(e){
var form = $(this);
$.post(
form.attr("action"),
form.serialize()
).done(function(data){
form.trigger("reset");
})
return false;
});
JS;
$this->registerJs($script);
?>
Inside ProjectController.php I've created the action as follow:
public function actionSubscriber()
{
$subscriber_model = new SubscribeNewsletterForm();
$request = Yii::$app->request;
if($request->isAjax && $subscriber_model->load($request->post())){
$subscriber = new Subscriber([
'email' => $subscriber_model->email
]);
$subscriber->save();
}
}
Here's the Subscriber model code.
Subscriber.php
<?php
namespace frontend\models;
use yii\db\ActiveRecord;
class Subscriber extends ActiveRecord
{
public static function tableName()
{
return 'subscriber';
}
}
?>
frontend/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-frontend',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'frontend\controllers',
'components' => [
'request' => [
'csrfParam' => '_csrf-frontend',
],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
],
'session' => [
// this is the name of the session cookie used for login on the frontend
'name' => 'advanced-frontend',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
/*
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
*/
],
'params' => $params,
];
?>
With above code validation is working but i'm not able to save the email in database. Please tell what i'm doming wrong.
You need rules on your Model. Also, I always replace the generated tables names with the table prefix supported method. Also, I always like to use the Timestamp behavior to log when things are created or updated. Especially when your grabbing contact info for the use of leads, I would record the timestamps as well as their IP Address.
Subscriber.php
use yii\behaviors\TimestampBehavior;
// ...
/**
* #inheritdoc
*/
public static function tableName()
{
return '{{%subscriber}}';
}
/**
* #inheritdoc
*/
public function behaviors()
{
return [
TimestampBehavior::className(),
];
}
/**
* #inheritdoc
*/
public function rules()
{
return [
['email', 'filter', 'filter' => 'trim'],
['email', 'required'],
['email', 'email'],
['email', 'string', 'max' => 255],
['email', 'unique', 'targetClass' => '\common\models\Subscriber', 'message' => 'This email address has already been taken.'],
[['created_at', 'updated_at'], 'integer'],
];
}

Yii2 Invalid CAPTCHA action ID in module

I am getting the Invalid CAPTCHA action ID Exception in my custom contactus module. I managed to display the captcha but models validation rule throws the invalid action ID exception. Below is my code:
contactus/controllers/DefaultController.php
class DefaultController extends Controller
{
public function behaviors()
{
return [
'access' => [
'class' => \yii\filters\AccessControl::className(),
'rules' => [
[
'actions' => ['captcha','index'],
'allow' => true,
],
]
]
];
}
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
public function actionIndex()
{
$model = new ContactForm();
if ($model->load(Yii::$app->request->post()) && $model->contact(setting::ADMIN_EMAIL_ADDRESS)) {
Yii::$app->session->setFlash('contactFormSubmitted');
return $this->refresh();
} else {
return $this->render('index', [
'model' => $model,
]);
}
}
}
contactus/models/ContactForm.php
public function rules()
{
return [
// name, email, subject and body are required
[['name', 'email', 'subject', 'body','verifyCode'], 'required'],
// email has to be a valid email address
['email', 'email'],
// verifyCode needs to be entered correctly
['verifyCode', 'captcha','captchaAction'=>'default/captcha'],
];
}
contactus/views/default/index.php
<?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>
<?= $form->field($model, 'name') ?>
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'subject') ?>
<?= $form->field($model, 'body')->textArea(['rows' => 6]) ?>
<?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
'captchaAction' => 'default/captcha',
'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
]) ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>
</div>
<?php ActiveForm::end(); ?>
I get the below error:
Exception (Invalid Configuration) 'yii\base\InvalidConfigException' with message 'Invalid CAPTCHA action ID: default/captcha'in E:\wamp\www\yii-application\vendor\yiisoft\yii2\captcha\CaptchaValidator.php:81
Am I missing something?
You should modify your validation rule :
['verifyCode', 'captcha','captchaAction'=>'/contactus/default/captcha'],

PagingScroller in ExtJs 4.1 MVC grid implementation not triggering ajax request

I am trying to figure out a basic implementation of infinite scrolling in a ExtJs 4.1.0 grid.
I am working off this example http://ext4all.com/post/extjs-4-1-grid-infinite-scroll-in-mvc and at this point my code is practically identical to the example.
When my page loads, the grid is filled with the first page of data as the first ajax request fires and works as expected. However, when I scroll down to the bottom of the grid, nothing happens... I am expecting to see an ajax request fire, grabbing additional data (as in the example).
Any ideas?
Here is my code:
HTML
<html>
<head>
<link rel="stylesheet" type="text/css" href="./extjs-4.1.0/resources/css/ext-all.css">
<link rel="stylesheet" type="text/css" href="./styles.css">
<script type="text/javascript" src="./extjs-4.1.0/ext-all-debug.js"></script>
<script type="text/javascript" src="./test-scrolling.js"></script>
</head>
<body>
<div id="page">
<header>
<div class='wrap'>
<h1>Test</h1>
<div class='clear'></div>
</div>
</header>
<div class="content">
<div class="wrap">
<div id="main"></div>
</div>
</div>
</div>
</body>
</html>
Javascript
Ext.define('AP.model.Log', {
extend: 'Ext.data.Model',
fields: [
'Id',
'ProcessId',
'IndexRequest',
'AssetIndexStart',
'AssetIndexEnd',
'StartDate',
'EndDate']
});
Ext.define('AP.store.Log', {
extend: 'Ext.data.Store',
model: 'AP.model.Log',
autoLoad: true,
remoteSort: true,
buffered: true,
pageSize: 100,
proxy: {
type: 'ajax',
url: 'http://localhost/...',
limitParam: 'limit',
reader: {
type: 'json',
root: 'Log',
successProperty: 'Success'
}
}
});
Ext.define('AP.view.Log', {
extend: 'Ext.grid.Panel',
alias: 'widget.log',
title: 'Log',
store: 'Log',
initComponent: function () {
this.columns = [{
header: 'Index Start',
dataIndex: 'AssetIndexStart',
flex: 1
}, {
header: 'Index End',
dataIndex: 'AssetIndexEnd'
}, {
header: 'Start Date',
dataIndex: 'StartDate'
}, {
header: 'End Date',
dataIndex: 'EndDate'
}];
this.callParent(arguments);
}
});
Ext.define('AP.controller.Log', {
extend: 'Ext.app.Controller',
stores: ['Log'],
models: ['Log'],
views: ['Log'],
init: function () {}
});
Ext.Loader.setConfig({
enabled: true
});
Ext.application({
name: 'AP',
appFolder: 'app',
controllers: [
'Log'],
launch: function () {
Ext.widget('log', {
title: 'Log',
width: 600,
height: 400,
renderTo: 'main'
});
}
});