Route to page in webroot folder? - CakePHP - php

How can I define a route to a .php file in my webroot folder?
Thank you
Edit:
How can I render a file in the webroot folder from a controller? I need this because I am using Auth.

I'm guessing you're trying to password/Auth protect a file in the webroot folder. If so, you're on the wrong track. Files in the webroot are served as-is by default and are bypassing Cake entirely. Any file you put in the webroot is by definition "public". To protect it, you'll need to store it someplace else outside the webroot. You can then serve this file from a controller using the Media View. Your URL would look like, for example, /files/download/foo, which maps to FilesController::download('foo') via normal routes.

Related

Host, PHP, htaccess

I need some help.
I was reading the security recommendations of my hosting service and they say that ideally just put the
index file and files like css, js and img inside my root folder, and that all other files should be placed
off, that is, a level above.
I tried doing this in my tests, and I had some problems. The structure of the hosting folders is:
/
/htdocs
Inside /htdocs I put the index.php file and when accessing it through the url exemple.com/index.php works normally.
But putting other test files out of htdocs is what starts the problem. For example, if I have a file called contact.php
and I try to access it through the url exemple.com/contact.php I get the 404 error message.
So the question I have to ask is:
Is it possible to access url files that are outside of htdocs, or better to put all the files that will be accessed by the url inside
of htdocs and leave only configuration files outside this folder, like class, functions, database connection, etc?
And if it is possible to access the files by url, how would I rewrite these urls in htaccess?
and that all other files should be placed off
Yes, this is good practice. However, you're misunderstanding the implementation.
You can not directly access files outside the document root. But you can indirectly access them. I.e., the web server can't see them, but your programming code can.
Ideally, your site would use the front controller pattern. Here, your index.php file would serve every page of your app by intercepting every request and then routing it to the correct end point. I.e., you would never directly request /contact.php, you'd instead request /contact, which would get funneled to /index.php, which would load the required resources from outside the doc root.

MVC - CSS links

I am relatively new to this whole MVC structure for web apps.
Here is the structure of my project.
The file you see on the right of the picture is inside the 'Views' folder.
When I want to link a css file, why does the path have to be href="/css/stylePortfolio.css"
Why is it not href="../../public/css/stylePortfolio.css"?
What you are looking at, is the HTML that is sent to the users' browser. The browser does not know anything about the structure of your application. It simply reads that href link, and downloads the file from http://example.com<link>, where <link> is /css/main.css for example.
When configured correctly, the web root of your website is in your /public folder. That means, anything that a browser requests, is relative to your web root. Thus, if you want to link to a css file, you need to think of that link relatively to your projects web root, not relatively to your project root.
An example:
Say, you create a new project in /home/user/AwesomePhpProject.
Now, /home/user/AwesomePhpProject is called your project root.
In your project root, you create a directory, public. You configure that directory to be your web root, using VirtualHost (when using Apache) or the root directive (when using Nginx).
/home/user/AwesomePhpProject/public is now your web root.
When a browser requests /css/main.css, it will be directed to /css/main.css relative to your web root. In our case, that will be /home/user/AwesomePhpProject/public/css/main.css.
Most modern applications separate the project and web root, for security reasons.
As your index.php is inside the public folder, so all the views are loading in the public folder. That is why you should declare the CSS path from the public root. You can modify the path if necessary.
In this case, you can declare a global variable or constant your main controller with the path of your CSS folder
define('CSS_PATH', 'http://localhost/fab/public/css/');
Now use this like
<link rel="stylesheet" href="<?=CSS_PATH?>bootstrap.css">
It's because front-end links (like CSS and JS) are relative from current file you are in. And since your root is in public you have to specify URL from this directory (you can't link to any file in top level of root).
Why is it not href="../../public/css/stylePortfolio.css"?
Because entry point of your MVC is index.php from public folder. So all your css and js links should be relative to public folder

Restrict PHP files outside application folder in CodeIgniter?

How can I restrict PHP files from getting executed outside the application folder?
I am using CodeIgniter and I have heard it somewhere that this thing is possible in CodeIgniter.
Temporary, I have added request in htacess which will surpass each request from index file and will result in not found.
Add public folder, move index.php into it, inside index.php change application and system folder settings to ../application and ../system respectively. Point domain to public folder.

copying a zendframework project in to it's subfolder

Hi
I have a zendframework project. Now it is running in my domain (http://www.mydomain.com).
I want to modify some portion of the site. So I decided to copy all the files and folders to its sub folder (http://www.mydomain.com/zendwork). Is it possible?
I heard that there is a problem in the relative path? Thus so how I change the entire path in an easiest way?
Thanks in advance
Only the files under the "public" directory need be under your document root. The rest of the codebase ("application", "library", etc) can exist anywhere.
The APPLICATION_PATH constant and include path defined in the public/index.php file dictate where to find the rest of the application.
The other consideration is to make use of the BaseUrl view helper when linking to JavaScript, CSS, image and other "static" assets in your views, eg
// view.phtml
<img src="<?php echo $this->baseUrl('/images/foo.jpg') ?>" alt="foo">
To move your application then only involves moving the "public" contents and editing index.php.
Yes.its possible but u need to set the path in .htacess file.

How to create a anchor link to different .php file?

I'm new to php and hope you can make me figure out whether I'm trying to do anything impossible.
I have two folders 'public' (root directory), and 'library' (all php files here), these folders are in same level of folder hierachy. my public/index.php is basically loading another php file (say aa.php) which is in 'library' folder on the loadup. Now I need to create a anchor link to file call bb.php which is also inside library folder.
I tried create anchor as follows
echo " my bb file
But I'm getting 404 error saying localhost/bb.php can not be accessed. I guess this is because bb.php file is not with in root directory and server is preventing direct access to this file.
Please help me to overcome this problem.
Thank you
If library/ and public/ are at the same level, the webserver will not be able to serve the files in library/. Typically, files in a library directory would be included by files in the public web folder.
If you need to use the bb.php directly, you will have to move it to public or a folder within public. And then from within public/bb.php you can include library files
/* public/bb.php */
include("/path/to/webroot/library/file.php");
Move bb.php into the folder "public".

Categories