Well you probably need to do a little reading then.
I have not read this article, but this is the sort of content you want to look for. MVC or Model View Controller topics relating to Yii2. There is a few out there for Yii, and its good for getting the idea, but you really want your head in Yii2 now, not Yii 1.*.
Try this article out for a start.
Now to your problem.
I would recommend using everything out the box until you understand how it all works.
So stick with the SiteController do not change it to FileController
Now to start off with in @app/config/web.php
Add this in there, it is used for simplifying urls so youdomain.com/index.html will goto your index action, simple. Also a bit of setup for using /gii also, you might want that later so i left it in.
More information here: http://www.bsourcecode.com/yiiframework2/url-manager-in-yiiframework-2-0/#configure-url-rules
$config = [
... , // Do not include this .. its to show other content might be here.
'components' => [
... , // Do not include this
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'suffix' => '.html',
//'cache' => 'cache',
//'scriptUrl' => '',
'baseUrl' => '/',
//'hostInfo' => 'http://www.yourhost.com.au',
'routeParam' => 'r',
'ruleConfig' => [
'class' => 'yii\web\UrlRule'
],
'rules' => array(
[
'pattern' => 'gii',
'suffix' => '',
'route' => 'gii',
],
[
'pattern' => '/<controller:\w+>',
'suffix' => '',
'route' => 'gii/<controller>',
],
[
'pattern' => '/<controller:\w+>/<action:\w+>',
'suffix' => '',
'route' => 'gii/<controller>/<action>',
],
'' => 'site/index',
[
'pattern' => '<action:\w+>',
'suffix' => '.html',
'route' => 'site/<action>',
],
),
],
... , // Do not include this
]
];
Secondly you need to add a .htaccess file to @web/.htaccess
This is so all requests get redirected to your index.php file again so Yii2 can look at the query you made.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php$1 [L,NC]
</IfModule>
Now for @app/controllers/SiteController.php
In this file you basically had what you needed, but I am including more options for you to play with.
So add the following maybe below the actionIndex() function.
Also check out http://www.yiiframework.com/doc-2.0/guide-runtime-responses.html
public function actionLog()
{
if (Yii::$app->request->isAjax) {
$data = Yii::$app->request->post();
$filename = $data['name'];
//$log = new History();
//$log->name = $filename;
//$log->save();
$response = Yii::$app->response;
$response->format = \yii\web\Response::FORMAT_JSON;
$response->data = ['filename' => $filename];
$response->statusCode = 200;
return $response;
}
else throw new \yii\web\BadRequestHttpException;
}
And finally in a view file, I am using the index file @app/views/site/index.php
Add this anywhere in the file, it is just a simple ajax link.
<?php
use yii\helpers\Html;
use yii\helpers\Url;
echo Html::a('Ajax Link Label','#', [
'title' => 'Ajax Title',
'onclick'=>"
$.ajax({
type :'POST',
cache : false,
data: {name: 'myfilename'},
url : '".Url::to(['site/log'])."',
success : function(response) {
console.log('success: '+response.filename);
},
error: function(){
console.log('failure');
}
});return false;",
]);
?>
Now when you goto yourdomain/index.html and click that link you will get a response in the console of "success: myfilename".
But if you went to yourdomain/log.html you will notice you get a "Bad Request (#400)" error because you are trying to access the log action directly and not via ajax. That is because we throw that exception in the log action.
[i]
NOTE: Now just as a disclaimer, I would not be making any important changes to databases or the like based on an ajax call. This is client side stuff so you need to make sure the controller action is protected from any sort of injection.[/i]
I hope that is all clear enough for you,
Noddy.