Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php MVC without routes

I would like to know if it's possible to create an mvc project in php without to use routes. For example:

I have the controller strumenti.php

class Strumenti
{
    public function index()
    { 
        require 'application/models/strumentimodel.php';
        $strumenti_model=new StrumentiModel("r");
        $strumenti = $strumenti_model->getAllInstruments();
        require 'application/views/_templates/header.php';
        require 'application/views/strumenti/index.php';
        require 'application/views/_templates/footer.php';
    }

    public function deleteStrumento($nome)
    {   
        if (isset($nome)) {
            require 'application/models/strumentimodel.php';
            $strumenti_model=new StrumentiModel("r");  
            $strum=$strumenti_model->deleteStrumentoDaArray($nome);   
        }
        header('location: 'mysite/strumenti/index');
    }
}

and my model strumentimodel.php

class StrumentiModel
{
    private $handle;

    function __construct($mode) {
        try {
            $this->handle = fopen(STRUMENTI, "$mode");
        } catch (PDOException $e) {
            exit('Errore di apertura file');
        }
    }

    public function getAllInstruments()
    {
        $csv = array();
        $lines = file(STRUMENTI);

        foreach ($lines as $key => $value)
        {
            $csv[$key] = str_getcsv($value,";");
        }
        return $csv;
    }
    public function deleteStrumentoDaArray($nome)
    {
        //array con tutti gli strumenti
        $strum=$this->getAllInstruments();

        for($i=0;$i<count($strum);$i++){
            if(in_array($nome,$strum[$i])){
                $this->indice=$i;
            }
        }
        unset($strum[$this->indice]);
        return $strum;
    }
}

and this is a View (index.php)

<div>
            <h3>Strumenti</h3>
                <table>
                    <tr>
                        <td>nome</td>
                        <td>modello</td>
                        <td>tipo</td>
                        <td>costo</td>
                        <td>Elimina</td>
                        <td>Modifica</td>
                    </tr>
                    <tbody>
                    <?php for ($riga=1;$riga<count($strumenti);$riga++):  ?>
                        <tr>
                            <?php for ($colonna=0; $colonna<count(current($strumenti)); $colonna++):  ?>
                                <td><?php echo $strumenti[$riga][$colonna];?></td>
                            <?php endfor; ?>
                            <td><a href="<?php echo mysite/strumenti/deleteStrumento/' . $strumenti[$riga][0]; ?>">x</a></td>
                            <td><a href="<?php echo mysite/strumenti/index ?>">Index</a></td>
                        </tr>
                    <?php endfor; ?>
                    </tbody>
                </table>



        </div>

If I would call the model from the controller there is no problems even without routes, but I can I call the controller from the view without routes?

In my example I call it so from a link:

<a href="<?php echo mysite/strumenti/deleteStrumento/' . $strumenti[$riga][0]; ?>">x</a>

The structure is: class/method/paramether

Is it possible to call the class method without routes?

like image 273
m.Sarto Avatar asked Dec 10 '25 16:12

m.Sarto


1 Answers

"Routes" have nothing particularly to do with MVC. A "route" as it is colloquially understood is some function/class/code that resolves a URL to an executable piece of code, usually a controller. Well, you can have that "for free" with PHP's standard behaviour by using URLs like /controllers/foo-controller.php, in which is code which will execute a controller. Your controller doesn't even need to be a class, it just needs to be something which receives a request and can decide which model actions to invoke and/or what view/response to send.

That's all MVC is about: having a core model which contains everything your app can "do", having views separately from that which provide a UI for your model, and lastly having a controller which acts on user input (here: HTTP requests). That separation is simply so you can adapt your app (the model) to different scenarios easily by swapping out the UI (view) and input handlers (controllers). Nothing in that prescribes the use of classes, routes or anything else.

like image 53
deceze Avatar answered Dec 13 '25 06:12

deceze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!