V
V
Vadim Stepanenko2018-03-14 14:04:09
PHP
Vadim Stepanenko, 2018-03-14 14:04:09

How wrong (terribly) is my organization on the site?

Hello! A week ago, I started to make a small project, 50-60% is already ready, and after seeing what happens, I became a little scared.
I have a few doubts and fears:
1. I make layout without a framework. Did some routing (if you can call it that). In general, in fact, the site will not be very large, at most 10 pages. The essence of the router is as follows:

switch ($_SERVER['REQUEST_URI']) {

    case '/':
    include_once 'modules/main.php';
    break;

    case '/deposit':
    include_once 'modules/load.php';
    break;

    case '/account':
    include_once 'modules/account.php';
    break;

    case '/faq':
    include_once 'modules/faq.php';
    break;

    default:
    echo "404";
  }

Based on this, how can you comment on such an implementation? What can I encounter using "this is this"?
2. As you can see from point 1, I will have 4 pages. And each template (well, as a template, the php file of the page) will be stored in the modules folder. I also ask you to comment on this implementation. It seems to me that something is done wrong in a more or less normal project
3. . Frequent include. I put such site elements as a header, sidebar, footer, footer into separate php files. Also, on some templates, php files with the necessary page functions are connected. Roughly speaking, there can be 5-6 inserts on one page. Isn't it bad?
4.There are almost no functions used in the code. I did the following: on the page you need to display the last 10 records in the chat. On the "account" page, you need to display the user's rating. I stupidly opened the php tag before and wrote the logic there. On the "wall" page, I also stupidly wrote code for caching records before. There was an idea to put all this logic from all pages into a separate file, format it as functions and request this file on each page, but why, if then there will be extra weight for loading?
5.. I use authorization through VK. After successful authorization, the session is assigned a VK user ID: $_SESSION['vk_id'] = '1'; and this id is stored in the database. But recently I studied someone else's project and there the hash was stored in the database, and the session, respectively, is also stored not stupidly ID, but hashed ID. Why do they do this, because as far as I know, the session cannot be replaced?
Thanks to everyone who will respond. Please do not scold me for such shit decisions, I started studying php recently and this is the first more or less large project

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Maxim, 2018-03-14
@Mx21

Look better https://www.youtube.com/playlist?list=PLE20id3DjfF...
The author, analyzes how to write your own framework. Based on these videos, you can understand how routing works in popular frameworks, template engines, mvc, etc.

D
Dmitry Kuznetsov, 2018-03-14
@dima9595

Read the basics of MVC, I think many questions will disappear at once.

X
xmoonlight, 2018-03-14
@xmoonlight

//можно подключить в отдельном файле....
function error_404() {
           header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); 
           header("Status: 404 Not Found"); 
           include(ERRORS_PATH.'/404.php');
           die();
}

switch (true) {
    case $_SERVER['REQUEST_URI']==='/':
    include_once 'modules/main.php';
    break;

    case strpos($_SERVER['REQUEST_URI'],'/deposit')===0:
    include_once 'modules/load.php';
    break;

  ... 
  ... 

    default:
    error_404();
  }

This is better because you can pass parameters to each application in a GET request through slashes and use it both via POST / GET-AJAX and in regular GET requests.
For example: /deposit/show/last/10
And we take the last ones (show - show , last - last, 10 - pieces) already in the "deposit" application.

T
ThunderCat, 2018-03-14
@ThunderCat

instead of a switch / case, it is better to use an array of the key-value type, it is also easier to programmatically increase (routing for 20 paths with a switch will be a so-so solution), and you can process it in any way, because it's data, not a condition. But in general, you were rightly told - see ready-made solutions, there are many of them, from simple to very fancy ones.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question