How to make a render in a model, and in General is it right?

Hey all, question on site architecture, became you think about it only after the controller was similar to the documentation for yii, well, in a volume sense. The website on AJAX, and it has this logic
// My page
 public function actionIndex()
{

 //If came from an AJAX request
 if (Yii::$app->request->isAjax) {

 //If id come
 if (Yii::$app->request->post('id')) {
 $id = Yii::$app->request->post('id');

 //About me sidebar
 if ($id == 11) {

 //The template content, without sidebar
 $this->layout = 'admin_content_ajx';

 return $this->render('index');
}

 //Personal information (phone, email)
 //Render only the lower part of
 if ($id == 1) {

 return $this->renderAjax('index');
}

 //My products
 if ($id == 2) {

 $query = Product::find()->with('image')->orderBy(['id' => SORT_DESC]);

 $pages = new Pagination([
 'totalCount' => $query->count(), 'pageSize' => 5,
 'forcePageParam' => false, 'pageSizeParam' => false,
 'route' => 'admin/feed/my-products'
]);
 $products = $query->offset($pages->offset)->limit($pages->limit)->all();
 return $this->renderAjax('my_products',compact('products','pages'));
}

 //My posts
 if ($id == 3) {

 return $this->renderAjax('message');
}

}

 //If none of this, to cancel the request
 return false;

}

 //Render the entire page About me
 return $this->render('index');
 }


And she's in almost every exene. I may be all wrong originally planned(For Cycling) to change, a little late, but if someone will give good advice on the architecture of the AJAX site, I would be grateful.

So the main question, I was thinking maybe to make the ajax logic in the model?
//If came from an AJAX request
 if (Yii::$app->request->isAjax) {

 //If id come
 if (Yii::$app->request->post('id')) {
 $id = Yii::$app->request->post('id');

 //About me sidebar
 if ($id == 11) {

 //The template content, without sidebar
 $this->layout = 'admin_content_ajx';

 return $this->render('index');
}

 //Personal information (phone, email)
 //Render only the lower part of
 if ($id == 1) {

 return $this->renderAjax('index');
}

 //My products
 if ($id == 2) {

 $query = Product::find()->with('image')->orderBy(['id' => SORT_DESC]);

 $pages = new Pagination([
 'totalCount' => $query->count(), 'pageSize' => 5,
 'forcePageParam' => false, 'pageSizeParam' => false,
 'route' => 'admin/feed/my-products'
]);
 $products = $query->offset($pages->offset)->limit($pages->limit)->all();
 return $this->renderAjax('my_products',compact('products','pages'));
}

 //My posts
 if ($id == 3) {

 return $this->renderAjax('message');
}

}

 //If none of this, to cancel the request
 return false;

 }


Tried, like all the rules, but the question is how to make a render of the model? And in General is it right?
class Model extends AdminNavigation
{

 public $nav_page = 1;

 public function __construct($number)
{
 $this->nav_page = $number;
}


 public function OwmNav() {

 if ($this->nav_page == 1) {

 return $this->render("index");;
}

 if ($this->nav_page == 2) {
 return $this->renderAjax("index");
}


}


}


In $this to pass if the render in the model is?
June 10th 19 at 15:24
2 answers
June 10th 19 at 15:26
Solution
Tried, like all the rules, but the question is how to make a render of the model? And in General is it right?

Wrong, it's against the principles of MVC on the basis of which is built freimark
became you think about it only after the controller was similar to the documentation for yii, well, the volume I mean

Excuse me, but You have the controllers bad code. You have one shooter responsible for a lot of vesani depending on some conditions. Split your code into separate actions what would each of them responsible only for their
Understood) Well about the bad code I guessed when I looked at it all) And a bunch of Aksenov in the controller is this normal?Do not prompt where it is possible more in detail about architecture to read, all that gives Google's Model is fatter, the Controller - you don't need to implement any logic, the types should not load anything from the database. What why and how, and that affects the details are not found, and there is no clarity in these matters, this bad code) - Gretchen_Daniel commented on June 10th 19 at 15:29
a lot of action is fine, better than a lot of small routines than large one.
Good architecture is based on OOP principles, so I recommend to start with it, you can read
Bertrand Meyer - Object-oriented design of software systems
Robert Martin agile programs
when the basic principles you can understand about patterns of something to read, for example:
Gang of four - the techniques of object-oriented design, design patterns
Martin Fowler Architecture of corporate program applications - bryana.Renn commented on June 10th 19 at 15:32
Thank you , be sure to study) - Gretchen_Daniel commented on June 10th 19 at 15:35
June 10th 19 at 15:28
Solution
How to make a render in a model, and in General is it right?

no, you can not do, and cannot do what You have done in the controller. Why do You have all in 1 action? I would have made an ajax call to a separate controller. A different action to break up exactly right.
If for some magical reason, you need all in a single action stuff, although I can't see that, then at least functions to do type
public function actionIndex($some_id){
switch($some_id){
 case 1:
 $rend = $this->getSomeOne();
break;
 case 2: 
 $rend = $this->getSomeTwo();
break;
 default: 
 throw new \yii\web\HttpException(404, 'The requested Item could not be found.');
}
 return $rend;
}

public function getSomeOne(){
 if($model = Some::find()->all())
 return $this->renderAjax('_some_one',['model'=>$model]);
 throw new \yii\web\HttpException(404, 'The requested Item could not be found.');
}

Even so, although I would do different action and may have scattered in different controllers
Yes that's that twists it) didn't expected that the site will be AJAX, is on the move up(So do certainly not must be, he used to think before doing, but it really happened) so I with minimal knowledge and understanding of architecture have created such a miracle)

I would have made an ajax call to a separate controller. A different action to break up exactly right.


That is, I have let's say 3-4 main controller, say a Personal web page, Store, Users.

Implement them like normal get requests(They also should be?) in case AJax lagane
class Controller extends OwnController
{

public function actionIndex()
{
 return $this->render('index');
}

}


And for each of them to create your Controller for the Ajax requests
class Controller extends OwnAjxController
{

public function actionIndex()
{

 //If came from an AJAX request
 if (Yii::$app->request->isAjax) {
 return $this->renderAjax('index');
}

}


}


but in js to specify where to send each button is pressed
$('body').on('click', '.content_nav>.my_feed_ajx', function () {



//Request
$.ajax({
 type: 'POST',
 url: '/admin/own-ajx/index',
 dataType: "html",
 success: function (data) {

 if (jQuery('.my_own_info').html(data)) {
 window.history.replacestate will('admin', null, '/admin/own/index');
}






},
 error: function () {

$(location).attr('href','/admin/own/index');

}

});



});


Feel drown in the number of controllers, action and Js functions, and if something will need to change, you have razvorotit the whole project( But also from the point of view of performance and correctness of writing code, it would be more correct? - Gretchen_Daniel commented on June 10th 19 at 15:31
,
Implement them like normal get requests(They also should be?) in case AJax lagane

What do you mean lagone and what's normal get?
Part of action games do You have for common queries, part of the ajax it is not necessary to live close and know about each other.
Or You website in the pjax was covered? Then you shouldn't have to do that - bryana.Renn commented on June 10th 19 at 15:34
To Pjax not wrapped, but put timout for ajax request, and if it passes, will change the link '/own/my-products'.
That is, if I pass by reference, /own,/my-products Controller loads the entire template, and if the page clicks the button of my products, then fill in ready-made form of a small piece of food and change the url to '/own/my-products', and again, if the user refreshes the page then get the whole page in its entirety. Probably in every controller just och a lot of rivet action, because to create for each controller, a separate controller for Ajax requests is problematic, and will spam the entire folder. So it will be better? - Gretchen_Daniel commented on June 10th 19 at 15:37
there are valid ulr /product/1 is the controller (product) and action (view) it handles get requests and displays the product. For example it is a unit that is necessary to update via ajax, he's not supposed to knock on the same url, make the url /ajax/product?id=1 (for ajax us CNC side) when the controller ajax and the action - product and it returns the proper unit, if the product with that id exists. With a frightened there is a situation in which this /ajax/product?id=1 it will be necessary to go through the usual get? - Gretchen_Daniel commented on June 10th 19 at 15:40
Not not all right, I'm just doing the substitution url in order to user the normal links to see and been able to go, and if the url /ajax/product?id=1 for example doesn't work, or och a long time to think will just make the transition to ulr /product/1

$('body').on('click', '.content_nav>.my_feed_ajx', function () {



//Request
$.ajax({
 type: 'POST',
 url: '/admin/own-ajx/index',
 dataType: "html",
 success: function (data) {

 //If return view
 if (jQuery('.my_own_info').html(data)) {
 //Substitute the url like I'm a regular query moved here
 //And if the user refreshes the page, the usual request will fly at this address
 window.history.replacestate will('admin', null, '/admin/own/index');
}

},

//If timeout has passed(not yet set)
 error: function () {
 the transition in the usual link 
$(location).attr('href','/admin/own/index');

}

});



});


With a frightened there is a situation in which this /ajax/product?id=1 it will be necessary to go through the usual get?

I kind of wrote about it, well, or not well-expressed thoughts.

Thanks to the help, I will restore order in your glove box) - Henderson_Beat commented on June 10th 19 at 15:43
then what You do is really had to be done via pjax for there all is already implemented. Timeout redirect in case of long delays, etc.
www.yiiframework.com/doc-2.0/yii-widgets-pjax.html - Gretchen_Daniel commented on June 10th 19 at 15:46
your lesson looked where you said that Pjax is not designed for such tasks, and it is better not to do it, manually or on JQ requests does not help the situation?) - Henderson_Beat commented on June 10th 19 at 15:49
You are trying to make pjax, it already is. When I say "Pjax not designed for such tasks," I mean exactly the task you are considering, but I don't remember what I would have considered what You are doing. So it's inappropriate citation.
Obviously by "this problem" you understand absolutely everything, whereas I was talking about specific things. I do not remember what, but this just didn't do. Because it just is pjax. If You want the whole site to do so - then this is the js framework and the type of angular. - Gretchen_Daniel commented on June 10th 19 at 15:52
,Your video, 24:00, I have just kind of situation when the whole site is on Ajax, well, you can and PJax to do, and Angular to teach probably the same as yii. That is, I'm the same Pjax trying to do?) - Henderson_Beat commented on June 10th 19 at 15:55
I'm so right there about Angular and analogues was saying.
That is, I'm the same Pjax trying to do?)
And that doesn't help. But it seems to me You whole project is not right doing. Why use ajax or pjax, or angular where it is not necessary? Why do You need it at all? The problem with simple get requests? What is the sacred meaning of all this? Explain, then maybe you'll be able to give advice. While I do not understand. - Gretchen_Daniel commented on June 10th 19 at 15:58
The request of the customer) and it was pure interesting to try as website in Ajax to do the meaning to something like the social network, your community somewhere on thousand people. With instant notifications, a forum, a shop and a couple of finalyse. But the more pyrhus, the more I realize that this is very hardcore, and I don't know yii2 is appropriate here at all?But sooo many new things I learned while trying to do all this, useful, interesting experience, I wish to finish before the end) There is a variant to convince the customer as with normal queries will live, but then again, I would like to end fight, since there is not difficult at all, except now and notifications. Since I will advise with the forum something to tell me? Saw Eliseevaintegration in yii, but have not tried it, in my mind the forum is kind of simple, but have not yet done. - Henderson_Beat commented on June 10th 19 at 16:01
I think I need the ajax part. For example, the swiper on the forum pages or products and then just pjax mega comfortable.
And if it is necessary or to take a ready-made framework or write your own micro framework. It pajax to use, but then in plain js without it yii involucre.
Yii in this case is more appropriate and how on the backend api and front-the front will be html+css+js with no php at all. - Gretchen_Daniel commented on June 10th 19 at 16:04
I understood , perhaps cut then really part of the unnecessary Ajax requests, but what conjured erase and will wrap better in Pjax. Thank you so much for your time and architecture explained at least sympathetic, and not only on it) - Henderson_Beat commented on June 10th 19 at 16:07
come again - Gretchen_Daniel commented on June 10th 19 at 16:10

Find more questions by tags Yii