Breadcrumbs logic in MVC applications

Where should the breading path be declared (in other words, in which MVC letter)? While I am declaring this in controllers, I recently started working with CakePHP, where everything is done in Views, and it surprised me.

+3
source share
5 answers

I am going to answer this question because there is a lot of confusion about what should and should not be done with breadcrumbs.

Model

A model is a layer that contains business logic that consists of domain objects, data cards, and services. Read more about the model here .

Controller

: , . / . , , , , , , .

The Breadcrumbs

, . :

:

  • ( /login ). , .
  • .
    • datamodel, . , .
    • , , , . , , - , .
  • , "class= current". "" , , , . , ( ), class=current, .

:

/**
 * So we're in the home controller, and index action
 *
 * @route localhost/home or localhost/home/index
 * (depending on .htaccess, routing mechanism etc)
 */
class Home extends Controller
{
    public function index()
    {
        // Get current class name, so we have the current page
        $class = __CLASS__;

        // Let say the page heirachy is tied to the model, so get pages for current method, or however you want to implement it
        $pages = $model->getPages($class);

        // So the TEMPLATE handles the breadcrumbs creation, and the controller passes it the data that it needs, retrieved from the model
        // (new Template) is PHP 5.4 constructor dereferencing, also included is some lovely method chaining
        // setBreadcrumbs() would assign variables to the view
        // Render would do just that
        (new Template)->setBreadcrumbs($currentPage, $pages)->render();
    }
}

, ... , PHP 5.4, ...

<body>
    <?php foreach($breadcrumbs as $b) { ?>
        <li <?=($b['current'])?'class="current"':''?>>
            <a href="<?=$b['page']['url']?>">
                <?=$b['page']['title']?>
            </a>
        </li>
    <?php } ?>
</body>

. , , , , .

googling "php mvc breadcrumbs", . !

+4

, "" "" , . Controller, - , , -, MVC.

0

. CodeIgniter -

$data['breadcrumb'][0] = array("title" => "Home", "alt" => "Home Page", "path" => "home/");
$data['breadcrumb'][1] = array("title" => "About", "alt" => "About Me", "path" => "home/about");

.

<ul class="breadcrumbs">
foreach($breadcrumb as $b)
{
    ?>
        <li><a href="<?php echo base_url(); ?>index.php/<?php echo $b['path'];?>" alt="<?php echo $b['alt']; ?>"><?php echo $b['title']; ?></a></li>
    <?php
}
</ul>

, , .. - .

ALL . , .

,

<?php echo ($loggedin)?"Logged in as" . $user->firstname:"Not logged in";?>

. . . . , . HTML, .

(, , , script , javascript ..), .

0

IMO, , , . , . , , , , , . , -, MVC , DRY >

, breadcrumb , , , url . , , , .

, . CakePHP ( PHP, ), , . , . .

0
$this->Html->addCrumb('Home', '/pages/home');
$this->Html->addCrumb('Contacts', '/pages/contacts');

echo $this->Html->getCrumbs('&raquo;', 'StartText');
0

Source: https://habr.com/ru/post/1749732/


All Articles