nezamy / route

Route - Fast, flexible routing for PHP, enabling you to quickly and easily build RESTful web applications.
http://nezamy.com/route
MIT License
214 stars 46 forks source link

Uncaught Error: Class No Found for Plates template #25

Closed EricFloat closed 6 years ago

EricFloat commented 6 years ago

I test your library with php plates templates.

first step: add plates in composer and install like this:

    "require": {
        "php": ">=5.4.0",
        "nezamy/support": "1.0.*",
        "league/plates": "3.*"
    },

add vendor autoloading in index and route controller using namespace:

require './vendor/autoload.php';

$route->get('/', 'Tests\App\Controller\test2Controller@post_getIndex');

Now add Plates template class in test2Controller@post_getIndex Like this:

namespace Tests\App\Controller;
class test2Controller{
    public function post_getIndex(){
       // Create new Plates instance
        $templates = new League\Plates\Engine('templates');
        // Render a template
        echo $templates->render('profile', ['name' => 'Jonathan']);
    }
}

But In Action I see This error and not found plates class:

Fatal error: Uncaught Error: Class 'Tests\App\Controller\League\Plates\Engine' not found in C:\xampp\htdocs\n\tests\app\controller\test2Controller.php:6 Stack trace: #0 [internal function]: Tests\App\Controller\test2Controller->post_getIndex() #1 C:\xampp\htdocs\n\system\Route.php(649): call_user_func_array(Array, Array) #2 C:\xampp\htdocs\n\system\Route.php(599): System\Route->callback(Array, Array) #3 C:\xampp\htdocs\n\index.php(8): System\Route->end() #4 {main} thrown in C:\xampp\htdocs\n\tests\app\controller\test2Controller.php on line 6

Can u help for this problem?!

yaayes commented 6 years ago

you need to use absolute namespace like below $template = new \League...

because if you see the error you will notice that the namespace being concatenated with the actual namespace:

Fatal error: Uncaught Error: Class 'Tests\App\Controller\League\Plates\Engine' not found

EricFloat commented 6 years ago

Oh! Gr8. Thanks

nezamy commented 6 years ago

@EricFloat also you can define that in main php file

require BASE_PATH.'vendor/autoload.php';

$app            = System\App::instance();
$app->request   = System\Request::instance();
$app->route     = System\Route::instance($app->request);
$app->view      = new League\Plates\Engine('templates');

The you can use it in where

<?php
namespace Tests\App\Controller;
class test2Controller{
    public function post_getIndex(){
    echo app('view')->render('profile', ['name' => 'Jonathan']);
    }
}
S3lfC0d3r commented 6 years ago

@nezamy : I check your method like this:

require '../vendor/autoload.php';
$app            = App\Core\System\App::instance();
$app->request   = App\Core\System\Request::instance();
$app->route     = App\Core\System\Route::instance($app->request);
$app->session   = new App\Core\Session\sessionDb('S3cUrE-S3Ssi0n');

and in controller:

namespace App\Catalog\Controller;
class IndexController extends \App\Core\Controller {

    public function index(){
          app('session')->regenerate_id();
    }
}

but see error:

Fatal error: Uncaught Error: Call to undefined function App\Catalog\Controller\app() in /Applications/MAMP/htdocs/mvc/application/Catalog/Controller/IndexController.php:14 Stack trace: #0 [internal function]: App\Catalog\Controller\IndexController->index() #1 /Applications/MAMP/htdocs/mvc/application/Core/System/Route.php(649): call_user_func_array(Array, Array) #2 /Applications/MAMP/htdocs/mvc/application/Core/System/Route.php(599): App\Core\System\Route->callback(Array, Array) #3 /Applications/MAMP/htdocs/mvc/public/index.php(56): App\Core\System\Route->end() #4 {main} thrown in /Applications/MAMP/htdocs/mvc/application/Catalog/Controller/IndexController.php on line 14

how do fix?!

nezamy commented 6 years ago

I see you are copied the files in your core you should be copy function.php too and include it in your main file

S3lfC0d3r commented 6 years ago

@nezamy : Oh Ok. I don have any include/require file and function to my mvc. can change to any class with php namespace and use method?!

nezamy commented 6 years ago

@S3lfC0d3r Just require once function.php in your main file

require '../vendor/autoload.php';
require_once 'app/core/system/function.php';
$app            = App\Core\System\App::instance();
$app->request   = App\Core\System\Request::instance();
$app->route     = App\Core\System\Route::instance($app->request);
$app->session   = new App\Core\Session\sessionDb('S3cUrE-S3Ssi0n');

OR create call app with namespace every single time

namespace App\Catalog\Controller;
class IndexController extends \App\Core\Controller {
   public $app;
   public function __construct() {
       $this->app = App\Core\System\App::instance();
   }
    public function index(){
          $this->app->session->regenerate_id();
    }
}
S3lfC0d3r commented 6 years ago

@nezamy : If create __construct function in IndexController we have override function for main Controller. Is not it better?!

add in Main Controller:

class Controller
{
    /** @var View View The view object */
    public $View;
    public $templates;
    public $app;
    /**
     * Construct the (base) controller. This happens when a real controller is constructed, like in
     */
    public function __construct()
    {
        $this->app = \App\Core\System\App::instance();
    }
}

And IndexController:

namespace App\Catalog\Controller;
class IndexController extends \App\Core\Controller {
    public function index(){
          $this->app->session->regenerate_id();
    }
}