I am trying to make my routes access to public folder as i am trying to use laravel as api and angular as frontend. I have changed the view file from config folder from
'paths' => [
realpath(base_path('resources/views')),
],
To
'paths' => [
realpath(base_path('public/views')),
],
in my public folder i have created a views folder and placed a file called test.html
Now
Route::get('/',function(){
return view('test.html');
});
it shows error
in FileViewFinder.php line 137
at FileViewFinder->findInPaths('test.html', array('C:\xampp\htdocs\customer_portal\public\views')) in FileViewFinder.php line 79
at FileViewFinder->find('test.html') in Factory.php line 165
at Factory->make('test.html', array(), array()) in helpers.php line 779
at view('test.html') in routes.php line 36
at RouteServiceProvider->{closure}()
Do you know what i am doing wrong?
The problem is that U use return view('test.html') on html file! Its wrong, view should be use for php blade templates. Instead return conent of your file (below code I copy-paste from my project FilesController):
use Response;
...
public function downloadFile(Request $request)
{
...
return Response::download(path_to_your_file, download_file_name);
}
and in routes.php file instead of Route::get('/',function(){... put:
Route::get('/', 'FilesController#downloadFile');
Related
I am using the image helper of 2.x in my cakephp 3.x application but i am facing a problem with this that in my cakephp 2.x the function return the value as below code
return $this->output(sprintf($this->Html->_tags['image'], $this->webroot($relfile), $this->Html->_parseAttributes($htmlAttributes, null, '', ' ')), $return);
but in cakephp 3.x output and _parseAttributes function is removed so i dont know how to use these two function in cakephp 3.x
_parseAttributes function contain the image related data like 'class', 'alt' other things
i have searched a lot on net for output function in cake 3.x but did not find any successfull solution please try to help me
Thanks
Use the Html helper
There's nothing in your example code that justifies the use of custom code for generating the tag, so you could simply use the Html helper instead
// ...
class YourCustomHelper extends Helper
{
public $helpers = [
'Html',
// ...
];
public function someMethod($relfile, array $htmlAttributes)
{
$options = $htmlAttributes + ['pathPrefix' => false];
return $this->Html->image($url, $options);
}
// ...
}
Note the use of the pathPrefix option, since it looks like you want to point to a custom path, you should disable it, as otherwise you may end up with the default image base URL being prepended to the path.
Custom tags and attributes
If you'd really need a custom solution that isn't already covered by other helpers, then the replacement for output() and _parseAttributes() are return and string templates.
For the sake of supplying an example, here's a trimmed down one of what HtmlHelper::image() does:
use Cake\Core\Configure;
use Cake\View\StringTemplateTrait;
// ...
class YourCustomHelper extends Helper
{
use StringTemplateTrait;
protected $_defaultConfig = [
'templates' => [
'someTemplate' => '<img src="{{url}}"{{attrs}}/>',
],
// ...
];
public $helpers = [
'Url',
// ...
];
public function someMethod($relfile, array $htmlAttributes = [])
{
$url = $this->Url->assetUrl($relfile);
$templater = $this->templater();
return $templater->format('someTemplate', [
'url' => $url,
'attrs' => $templater->formatAttributes($htmlAttributes),
]);
}
// ...
}
This should be pretty much self explantory, you include the StringTemplateTrait that holds the templating functionality, define a custom template, and format it using the templater.
Note the use of the Url helper, it will do all the necessary stuff like encoding the URL, timestamping it, adding the webroot path, etc...
See also
Cookbook > Views > Helpers > Configuration options
Source > \Cake\View\Helper\HtmlHelper::image()
API > \Cake\View\Helper\UrlHelper::assetUrl()
API > \Cake\View\StringTemplate::format()
API > \Cake\View\StringTemplate::formatAttributes()
Cookbook > Appendices > 3.0 Migration Guide > Helpers
I would like to attach an uploaded file (pdf) to a normal create function, but it doesn't really work. I have model, controller, form modified, and it says uploading, model validates, but still, no pdf in db. I have set field as blob. what can be wrong?
model:
use yii\web\UploadedFile;
class MyModel extends \yii\db\ActiveRecord {
public $file;
...
public function rules() {
return [
[['file'], 'file', 'skipOnEmpty' => true, 'extensions' => 'pdf'],
...
_form:
$form = ActiveForm::begin([
'enableClientValidation' => false,
'options' => ['enctype' => 'multipart/form-data'],
]
...
<?= $form->field($model, 'file')->fileInput() ?>
controller:
use yii\web\UploadedFile;
public function actionCreate() {
$model = new MyModel;
try {
if ($model->load($_POST)) {
$model->file = UploadedFile::getInstance($model, 'file');
if ($model->save()) {
...
This is how it works for file uploading
$model->file = UploadedFile::getInstance($model, 'file');//Get the uploaded file
$model->file->saveAs('uploads/' . $model->file->name);//Save uploaded file at your desired location
$user->image = $model->file->name;//image is my database field name,to only save the name of the uploaded file
I hope this will make it clear and solve your problem
For example, from within a Controller (this doesn't work):
$this->Components->load('api/UserComponent');
... calling a component from folder structure like so:
app/Controller/Component/api/UserComponent.php
try adding this into your bootstrap: it will tell cake to consider also your subdirectory when loading components from your controller
App::build(array(
'Controller/Component' => array(
APP.'Controller/Component/api/'
)
));
after that you should be able to include and use component as if it would be in Component directory
Solutions is two in CakePHP 2.x
Add now Path by App::build()
// Append Path in bootstrap.php
App::build(array(
'Controller/Component' => array(
APP . 'Controller'.DS.'Component'.DS.'Reports' . DS,
)
), App::APPEND);
// Load in controller MyController ....
public $components = array('Paginator', 'Session', 'ReportUsers');
// In controller
$this->ReportUsers->Demo('bla..bla..');
// OR load in fly in action form controller.
$this->ReportUsers = $this->Components->load('ReportUsers');
$this->ReportUsers->Demo('bla..bla..');
Or by CakePlugin, Create one Plugin Reports for example:
// Add Plugin in file bootstrap.php
CakePlugin::load('Reports');
// MyController use Example
// Load in controller MyController ....
public $components = array('Paginator', 'Session', 'Reports.ReportUsers');
// In controller
$this->ReportUsers->Demo('bla..bla..');
// OR load in fly in action form controller.
$this->ReportUsers = $this->Components->load('Reports.ReportUsers');
$this->ReportUsers->Demo('bla..bla..');
You can't have your components in a sub folder under Component folder. If you want to better organize your classes use plugins.
I'm using blueimp to upload images in cakephp 2.x. Images are upload but issue is after uploading images.I got error "SyntaxError: Unexpected token <" and json data in html format. I tried to figured it out myself and find a solution given on SO. I tried to resolve my issue by following answer given in solution but could not. (sorry for bad English)
my controller code
App::import('Vendor', 'UploadHandler', array('file' => 'file.upload/UploadHandler.php'));
$this->layout = 'upload';
$options = array(
// 'upload_dir' => WWW_ROOT . DS . 'img',
'accept_file_types' => '/\.(gif|jpe?g|png)$/i',
);
$upload_handler = new UploadHandler($options);
}
I changed url In main.js(blueimp js file)
// Initialize the jQuery File Upload widget:
$('#fileupload').fileupload({
// Uncomment the following to send cross-domain cookies:
//xhrFields: {withCredentials: true},
url: 'index'
});
json data displayed on index page.
{"files":[{"name":"1186225_383357368459160_1371777554_n.jpg","size":58938,"url":"http:\/\/localhost\/ec\/cakephp\/app\/webroot\/files\/1186225_383357368459160_1371777554_n.jpg","thumbnailUrl":"http:\/\/localhost\/ec\/cakephp\/app\/webroot\/files\/thumbnail\/1186225_383357368459160_1371777554_n.jpg","deleteUrl":"http:\/\/localhost\/ec\/cakephp\/app\/webroot\/?file=1186225_383357368459160_1371777554_n.jpg","deleteType":"DELETE"},{"name":"e0775308650b201469fc68765dc4ff7a.jpg","size":150919,"url":"http:\/\/localhost\/ec\/cakephp\/app\/webroot\/files\/e0775308650b201469fc68765dc4ff7a.jpg","thumbnailUrl":"http:\/\/localhost\/ec\/cakephp\/app\/webroot\/files\/thumbnail\/e0775308650b201469fc68765dc4ff7a.jpg","deleteUrl":"http:\/\/localhost\/ec\/cakephp\/app\/webroot\/?file=e0775308650b201469fc68765dc4ff7a.jpg","deleteType":"DELETE"}]}
i had the same problem.
it was the validate variable
wrong
public $validate;
correct
public $validate = array();
I am working on CakePHP and I have a URL http://admin.example.com/Pages .
How can I create http://admin.example.com/Pages.html ? Is there any solution or component to solve this issue?
According to CakeBook , You can define extensions you want to parse in routes
E.g. Write the code below in app/Config/routes.php
Router::parseExtensions('html');
This will allow you to send .html extenstion in routes(url)
Not create a link
E.g:
$this->Html->link('Link title', array(
'controller' => 'pages',
'action' => 'index',
'ext' => 'html'
));
When you click that link you will get url in browser something like this
http://admin.example.com/pages/index.html
Do you really need this? CakePHP is automatically render view files from View folder
You can create (if its PagesController and index methond) index.ctp in app/View/Pages folder and it will be automatically render.
You can use file_get_contents function to read files. Something like this:
// in controller
function index() {
$content = file_get_contents('path/to/file.html');
echo $content;
die();
}