mikecao / flightphp.com

Flight website
https://flightphp.com
78 stars 22 forks source link

Error call function from view #3

Closed kampunghosting closed 7 years ago

kampunghosting commented 7 years ago

hai, i make getBaseUrl() function in index. and i want to call function in view. but i get error ! ) ( ! ) Fatal error: Call to undefined function getBaseUrl() i
and, how to make custom library to framework?

mikecao commented 7 years ago

What does your code look like? A view is just a PHP page, so you could pass in your function like you do your template variables.

kampunghosting commented 7 years ago
`function getBaseUrl() 
{
    // output: /myproject/index.php
    $currentPath = $_SERVER['PHP_SELF']; 
    // output: Array ( [dirname] => /myproject [basename] => index.php [extension] => php [filename] => index ) 
    $pathInfo = pathinfo($currentPath); 
    // output: localhost
    $hostName = $_SERVER['HTTP_HOST']; 
  if(!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])){
      $protocol = $_SERVER['HTTP_X_FORWARDED_PROTO'].'://';
  }else{
    $protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https://'?'https://':'http://';
  }

  $cekdir =$pathInfo['dirname']==='/'?'/':$pathInfo['dirname'].'/';
    return $protocol.$hostName.$cekdir;
}`

and in view i try call function with function getBaseUrl() , but not work

mikecao commented 7 years ago

I mean how are you calling your function? You can't just call any function from a view. You need to pass the function to your template.

$getBaseUrl = 'getBaseUrl';

Flight::render('template.php', array('getBaseUrl' => $getBaseUrl));

Then in your template you can call the variable:

$getBaseUrl();

Or you can use PHP $GLOBALS to create a global variable.

arthurrc03 commented 7 years ago

Hi, I have this code:

Flight::render('header');

Flight::route('/', function(){ Flight::render('hello', array('title' => 'Bob')); });

My ask is this:

How can show the variable $title in the template "header", I only can show the variable $title in template "hello"

Thanks You.!

Regards!

kampunghosting commented 7 years ago

Arthur03: First you must render yout template header in route like this Flight::route('/', function(){ Flight::render('header', array('title' => 'Bob')); Flight::render('hello', array('title' => 'Bob')); });

I hope i can help you..