Cakephp 3.0 ROOT variable gets set wrong - cakephp-3.0

After a stock install and creation of a basic controller, the environment variables are being set wrong.
For example, ROOT gets set as follows:
/var/www/html/appname/src//var/www/html/appname
In other words, the Apache webserver DocumentRoot is being appended to it.
Why?

What I learned is that if I specify the absolute path to the file in a controller, the Apache DocumentRoot gets appended to the ROOT variable. For example:
public function getFile() {
// disable the default layout for the view
$this->viewBuilder()->layout(false);
$path = ROOT . '/file/file.txt';
$this->response->file($path, array('download' => true));
return $this->response;
}
The browser cannot find the file at location /var/www/html/app/src//var/www/html/app/src/file/file.txt
However the following works as expected:
public function getFile() {
// disable the default layout for the view
$this->viewBuilder()->layout(false);
$path = '/file/file.txt';
$this->response->file($path, array('download' => true));
return $this->response;
}
The ROOT location seems to get automatically pre-prended to the path I have set above. I was not aware of this and haven't read anything about it in the documentation, so I am still not sure why it works this way.

Related

Accessing config.yml variables in Bolt extensions

I've set up a clean extension and am trying to pull in info from the extension's config.yml file. Config.yml is placed inside the extension folder (at the same level as Extension.php).
At the moment I'm just testing to see if I can retrieve the config. Here's the whole Extension.php:
<?php
namespace Bolt\Extension\andyjessop\vimeo;
use Bolt\Events\CronEvent;
use Bolt\Events\CronEvents;
use Bolt\Application;
use Bolt\BaseExtension;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class Extension extends BaseExtension
{
public function initialize() {
$this->app->get('api/update_video_content', array($this, 'updateVideoContent'))
->bind('updateVideoContent');
}
public function getName()
{
return "vimeo";
}
public function updateVideoContent()
{
$config = $this->config['user_id'];
$response = $this->app->json($config);
return $response;
}
}
And in the config.yml:
access_token: xxxxxxxx
user_id: xxxx
api_base_url: https://api.vimeo.com/
But it returns an empty object. What am I doing wrong here?
Is your config called Config.yml.dist or config.yml.dist - note the capital C, it should be all lowercase? Other than that, after installation of your extension the config.yml.dist will be copied to app/config/extensions/{extensionname}.config.yml and the values in there will be used.

PHP namespace behaviour gives FATAL error with spl_autoload_register

I want to use namespace and spl_autoload_register together but failed with different error each time.
Please See complete code files on github.
Below are the files
a base file where create a class with namespace class.alpha.php
an include file where I define spl_autoload_register include.php
an example file which instantiate the class object eg.php
Now when I create object from eg.php it gives FATAL error but when I comment namespace line in class.alpha.php then it's working
Please see the code below.
alpha.class.php
<?php
//namespace Alpha; //<< comment and uncomment this to regenerate the error
class Alpha
{
// public static $baseDir_;
public $dir = __DIR__;
public static $baseDir_;
public function __construct()
{
echo __FILE__."=>".__METHOD__;
var_dump(self::$baseDir_, $this->dir);
$firstDir = !empty(self::$baseDir_) ? self::$baseDir_ : $this->dir;
}
}
include.php
<?php //namespace Alpha\config;
spl_autoload_extensions(".php");
spl_autoload_register('loadclass');
function loadclass($class)
{
try {
if (is_readable(strtolower($class).".class.php")) {
include_once strtolower($class).".class.php";
}
} catch (Exception $e) {
print "Exception:". $e;
}
}
//#link http://php.net/manual/en/function.spl-autoload-register.php
// spl_autoload_register(__NAMESPACE__.'Alpha\Alpha()' );
eg.php
<?php
require_once 'include.php';
/** below code works by commenting 1st line on alpha.class.php
if we un comment then below code gives Fatal error: Class 'Alpha' not found */
Alpha::$baseDir_ = '/opt/lampp/archive/';
$obj_ = new Alpha();
var_dump(get_included_files());
var_dump($obj_);
/** now we define namespace Alpha on alpha.class.php */
// $ns_ = new Alpha\Alpha(); // Fatal error: Class 'Alpha\Alpha' not found
// var_dump($ns_);
/** not working even with use statement */
// use Alpha;
// use Alpha;
// $fn = new Alpha\Alpha();
// var_dump($fn);
Please help me out to solve this issue.
Thanks
Your autoloader is receiving a request for a class of "Alpha\Alpha" if you uncomment the namespace in alpha.class.php and place the use Alpha\Alpha in eg. This means that the location it's expecting to find your class in would be alpha\alpha.class.php.
Unless you're on Windows, directory separators are typically forward slash (/). So there's a number of possible solutions.
**Possible Solution #1 - Leave all files in the same place **
If you want to leave everything where it is now, you'll need to remove the namespace from the class names in the autoloader. If you add these lines to the top of your autoloader, that will make it behave that way:
$classParts = explode("\\", $class);
$class = $classParts[count($classParts) - 1];
I would not recommend this solution though since it means that you can no longer provide the same class name in a different namespace.
Possible Solution #2 - Put namespaces in subdirectories
For this solution, you'd create a new directory "alpha" and move "alpha.class.php" into it. For autoloader changes, you can add the following lines to the top of your autoloader:
$class = str_replace("\\", "/", $class);
This will change the namespace separators from backslashes to file path separators with forward slash. This will work on windows as well as mac and linux.
Possible Solution #3 - Follow an established autoloading standard
There are already a number of standard PHP autoloading standards. PSR-0 (now deprecated) works, but PSR-4 would be recommended:
PSR-0: http://www.php-fig.org/psr/psr-0/
PSR-4: http://www.php-fig.org/psr/psr-4/
One big upside of following one of these standards is that there are already plenty of implementations for them and there's been a lot of thought put into how they should work and maintain compatibility with other libraries you may end up wanting to use. Composer (http://getcomposer.org) will allow you to set up and use both PSR-0 and PSR-4 style autoloaders based on a very simple configuration.
Anyway, for the TL;DR crowd, the issue is that the autoloader receives the entire namespaced path in order to know how to load the class. The fatal error was because the autoloader wasn't properly mapping from that namespaced class to a file system location, so the file containing the class was never being loaded.
Hope this helps.

How would I go about loading a JSON file using Laravel?

I have a JSON file that I'd like to load using Laravel. I'm learning Laravel and would like to know the right way to do this. I have the JSON files in a folder called json in the public folder.
In my routes.php I have the following:
Route::get('/json/{jsonfile}', array(
'as' => 'load-json',
'uses' => 'JSONController#loadJSON'
));
In JSONController I have:
public function loadJSON($jsonfile) {
// not sure what to do here
return View::make('json.display')
->with('jsonfile', $jsonfile);
}
Also is my naming convention ok or do you have better suggestions?
Always be careful when allowing a user inputed data to decide what files to read and write. Here is simple code that will take in the filename and look in the apps/storage/json folder. I'm less familiar with what Illuminate does to protect against system injections but you might want at the very least to make sure that 'filename' doesn't contain anything but alphanumeric characters with a validator.
Unless the JSON (or any file) needs to be public you shouldn't keep it in the public folder. This way they must go through your app (and permissions) to view it. Also you can have more restrictive permissions outside the public folder.
public function loadJSON($filename) {
$path = storage_path() . "/json/${filename}.json"; // ie: /var/www/laravel/app/storage/json/filename.json
if (!File::exists($path)) {
throw new Exception("Invalid File");
}
$file = File::get($path); // string
// Verify Validate JSON?
// Your other Stuff
}

asp.net MVC saving relative path in database as not the full path

asp.net MVC saving relative path in database as
~/Content/Uploads/11thMay.jpg
not the full path as
D:/Projects/HRMV2/CubicHRMWeb/Content/Uploads/11thMay.jpg
I wanted to save relative path but in DB full path was being saved.
How can I do it?
thanks.
If your question is "How to save as full path", please try this
string path = Path.Combine(Server.MapPath("~/Content/img/Company/LogoFiles"), filename);
If your question is "How to save as relative path", please try this
[HttpPost]
public ActionResult Save(HttpPostedFileBase myFile)
{
string filename = myFile.FileName;
string relativeFileName = "~/Content/Uploads/" + filename;// relative path
return View();
}
this code doesn't allow to save the relative path, when you try it, the totality of filename is setted and not just the filename.if you insert a breakpoint at this point, you must obtain a thing like
"/content/Uploads/C:Document/Picture/father.jpeg"
and not the relative path which you will want it like
"/content/Uploads/father.jpeg"
in order to save the relative path of a document , you ought to use the getfilename property of system.io as show in the below example.
I suppose that you want to do it in the post method of your controller which contains a model or httppostedfilebase
in my example, i have a model with httppostedfilebase property named imagefichier, so you can get the document name by doing this
var lefilename = System.IO.Path.GetFileName(model.imagefichier.FileName);
//Suppose that you want to save it in your root web file
//like ~/Images/Administrateur/leilename,you can do it like this
string cheminbdd = "~/Images/Administrateur/" + lefilename;
//it is cheminbdd that you can save in your database as your relative path

Laravel 3: Class not found in namespace

This is the problem: I haven't be able to load a class from a namespace.
I'm developing a restful app and I'm trying to follow the Entity/Service/Repository way to access and give format to the requested data. The thing is that I cannot load any class from Services. I have created a folder inside my app called api, and within it the others 3 folders: api/entities/, api/services/ and api/repositories/. There are 2 more folders inside services: validators and datapickers.
As it is a RESTulf app, I also created an api folder inside the controllers folde: controllers/api/.
Here is the the current tree of my app folder:
app/
api/
entities/
repositories/
services/
datapickers/
MemberData.php
ConsumeData.php
validators/
...
models/
controllers/
api/
members.php
(other restful controllers)
languages/
...
In first instance, this is my Autoload section from start.php:
Autoloader::directories(array(
path('app').'models',
path('app').'libraries',
path('app').'api'
));
Autoloader::namespaces(array(
'Api' => path('app').'api',
));
And this is MemberData.php:
<?php
namespace Api\Services\Datapickers;
use Api\Repositories as Repo;
class MemberData
{
/* Do some stuff */
}
Now, when I try to use MemberData in controllers/api/members.php:
<?php
use Api\Services\Datapickers\MemberData;
class Api_Members_Controller extends Api_Base_Controller
{
public function get_index($id = null)
{
if (!empty($id))
$this->params->data = MemberData::getById($id);
else
{
$this->params->data = MemberData::getAll(
Input::get('offset'),
Input::get('limit')
);
}
return Response::json($this->params, 200);
}
}
I get the following:
Unhandled Exception
Message:
Class 'Api\Services\Datapickers\MemberData' not found
Location:
/path_to_project/application/controllers/api/members.php on line 15
which is this line: $this->params->data = MemberData::getById($id);
I autoload the api folder and register the base api namespace in start.php but I still keep receiving the same error. IT's like Laravel doesn't recognize the api namespace or something like that.
I tried to register the full namespace:
Autoloader::namespaces(array(
'Api\Services\Datapickers' => path('app').'api/services/datapickers',
));
but the error I got was: Call to undefinde method 'Api\Services\Datapickers\MemberData::getById()'. This was just a test (I don't want to register every sub-namespace in the Autoloader).