PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 31, 2022

[FIXED] Including a non-class php file via namespace without using relative path

 January 31, 2022     composer-php, include, namespaces, path, php     No comments   

Issue

The problem I have been having for some time is including a "view" php file that resides in some location, known to the class whose method I am calling, but not necessarily to the place from which I am calling it. Best to explain it by example:

Say I have a file being included somewhere, that is responsible for rendering a table. Because there are many similar tables in my application, I use a separated file with template-like table view and if I want to change some aspects of it, I pass the needed parameters.

Here is said concrete view:

[MainView.php]


// Convert data from JSON.
/** @var $model MainDataModel */
$model = MainDataModel::rebuildFromJson($_POST["model"]);

// Prepare data for table view
/** @var $data TableViewData */
$data = new TableViewData();
$data->model = $model;
$data->tableBody = "MainTableBody.php";
$data->tableClass = "main-table";
$data->viewClass = "main-view";

/** Attach table. */
// include("../../Common/View/TableView.php"); (1)
include("../../../../vendor/composer/wb/Common/src/View/TableView.php"); (2)

and here is common TableView I want to pass the $data to and include to page:

[TableView.php]
namespace Common\Controller;

use ...

/** @var $data TableViewData  */
if (!(isset($data) && $data->checkForRequiredFields($data))) {
    return;
}

?>
<div class="<?php echo $data->viewClass; ?>">
    <div class="datatable">
        <table class="<?php echo $data->tableClass; ?>">
            <thead>
                <tr>
                <?php /** @var $column ColumnModel */
                    foreach ($data->model->columns as $column): ?>
                       <th><?php echo $column->text ?></th>
                <?php endforeach; ?>
                </tr>
            </thead>
            <tbody class="datatable-body">
                <?php include($data->tableBody); ?>
            </tbody>
         </table>
    </div>
</div>

All is fine (relatively) as long as I use include (1), but when I switched to composer I also separated the common classes and put them in some remote location, that I really should not care about.

In other words, something tells me I shouldn't need to have knowledge about exact path to TableView.php, relative to wherever I am using it and there probably has to be a better way to use it. Obviously, (2) doesn't quite work and I would actually be surprised if such a foolish way would have worked.

So now my question is: how could I include that view (TableView) without using relative path every time I use it? Something along the lines of including and accessing controller class (TableViewController?), which is in same directory as TableView and holds information about absolute positioning of the TableView?

Some way around the problem is also to make the class return the view and then call the class by namespace, but that's workaround, not the solution.


Solution

After playing around with various path functions and constants I've come to some solution of a problem. It's not too neat but nevertheless it works. Here it is:

[TableViewController.php]
(...)
public static function getTableViewPath() {
        return __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "View" . DIRECTORY_SEPARATOR
            . "TableView.php";
    }  
(...)

[MainView.php]
(...)
/** Attach table. */
include(TableViewPresenter::getTableViewPath());
(...)


Answered By - mareckmareck
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing