Zend Framework 2 getQuery() returns null - zend-framework2

I am learning about the request class.
I have this url http://localahost/blog/search?year=2013&month=07
When i try print_r($_GET) it returns the array as expected.
When i try print_r($request->getQuery());
It returns an empty object Zend\Stdlib\Parameters Object ( [storage:ArrayObject:private] => Array ( ) )
So echo $request->getQuery()->year; returns nothing...
My .htaccess (if needed)
RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# The following rewrites all other queries to index.php. The
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]

WHat you are doing as Mentioned in your Comments was that you are Doing
$request= new $request();
What you need to know is that when you extend a class with AbstractActionController you dont need to do $request= new $request(). AbstractActionController provides you
getRequest()
from which you can fetch request and its data.

Related

How to remove index.php from url in Laravel 5.1?

i am trying to run my project on client's server with the following url:
http://quotes.simplybridal.com/simply-bridal-backend/login // It Gives 404 Not Found Error
But it works for:
http://quotes.simplybridal.com/simply-bridal-backend/public/index.php/login // It works
now, my question is how can we remove this index.php from the url.
here is my .htaccess file content, which i placed outside /public folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /simply-bridal-backend/
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Any help would be really appreciated.
Looks like an Apache issue.
Can you post your Virtual Host content (or .htaccess)?
Check your document root variable (that should be something like /foo/bar/public (with no trailing slash)
--
Put this default .htaccess and make sure the route exists and it is reachable.
Options -MultiViews
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Zend Framework 2 without Vhost configuration

I had finished my first web application using Zend Framework 2 and I'm about to put it online.
But may web host doesn't allow me to change my vhost configuration!
The .htaccess file is allowed.
So my question is: How to set up my ZF2 app with only .htaccess files?
Assuming you have a standard ZF2 application based of the skeleton, then try creating a .htaccess file in the root with this in it:
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index.php [NC,L]
I haven't tested this, but in theory, this should effectively move the document root to the public directory while keeping it out of the url.

.htaccess rewrite frustrations

I made my website in PHP originally and used parameters for page/product identification.
ex.) http://mysite.com/?pid=my_products&sid=golf_clubs&GTIN=111111111111
I've now moved over to using RoR and I'd like to set up redirects to clean up the urls
http://mysite.com/my_products/golf_clubs/111111111111
I've looked at tutorials and arrived at this:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ ?pid=$1&sid=$2&GTIN=$3 [NC]
I've tested a url and it doesn't redirect at all. I've tried putting this in the .htaccess file to debug and chmodded the log folder to 775 but I get a 500 error:
RewriteLog "/home/myaccount/rails_apps/myapp/log/rewrite.log"
RewriteLogLevel 3
As far as rewrite statements in .htaccess goes, this is what I have:
RewriteEngine On
RewriteLog "/home/myaccount/rails_apps/myapp/log/rewrite.log"
RewriteLogLevel 3
RewriteBase /
RewriteCond %{REQUEST_URI} ^/downloads.*
RewriteRule .* - [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ pid=$1&sid=$2&GTIN=$3 [NC]
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"
* I am on a shared hosting plan with Bluehost (good gawd I wish they'd update to Rails 3 soon)
If I understand you correctly, you have your rule backwards. You want to catch your old URLs and rewrite them to the new format, correct? In that case, you want this:
RewriteRule ^\?pid=(\w+)\&sid=(\w+)\&GTIN=(\w+) /$1/$2/$3 [NC,R=301,L]
Adding a 301 status will tell the user agent (including well-behaved search bots) that your canonical URLs have permanently changed.

Symfony 1.4 Error 400 on the home page in prod env (not in dev env)

I have made an tested an application on my computer and all is working fine (I used only the dev environment via the frontend_dev.php page).
When I deploy this application on the test server, I have the error 400 Bad Request below:
Bad Request
Your browser sent a request that this server could not understand.
I accessed to the website with the normal URL.
I'm using symfony 1.4 with doctrine (no others plugins are involved), wamp 2 (php 5.3, apache 2.0.59).
If I try to access the application with the dev environment, it works.
Try checking if the no_script property on your application setting.yml file. If should be set to False, clear cache and try again
Thank you very much Guiman. You have leaded me to the answer.
The no_script_name property wasn't the origin of the problem. Instead, I have my .htaccess in the web directory which was badly generated (I swear on my pet's head that I haven't edited this file before). Below is the file generated by symfony (the bold attribute isn't working within the code one, look at the last line):
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
# uncomment the following line, if you are having trouble
# getting no_script_name to work
#RewriteBase /
# we skip all files with .something
#RewriteCond %{REQUEST_URI} \..+$
#RewriteCond %{REQUEST_URI} !\.html$
#RewriteRule .* - [L]
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
And this is the updated one (again, look at the last line):
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
# uncomment the following line, if you are having trouble
# getting no_script_name to work
#RewriteBase /
# we skip all files with .something
#RewriteCond %{REQUEST_URI} \..+$
#RewriteCond %{REQUEST_URI} !\.html$
#RewriteRule .* - [L]
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ **/myApp/index.php** [QSA,L]
</IfModule>
I don't know what happened but I will check on the symfony website I there are similar issues.

Redirect "mydomain.com/user" to "mydomain.com/name.php?id=user"

I think I can use the .htaccess file for this, but I've looked it up and not found anything useful. What I want to do is have my site redirect to a php page when a user types their username in the URL like:
example.com/username
And have it be the same as a PHP page like:
example.com/name.php?id=username
I'd like it to display as example.com/username even after it redirects, but it is not necessary. Any ideas?
You can use mod_rewrite to transparently rewrite your URLs on the server.
Assuming that you'd only have usernames following your domain, something like this would do what you want:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .+ name.php?id=$0
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) users.php?user=$1 [L]
I think that will work.
The Apache mod_rewrite guide is here
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
You probably want Apache's mod_rewrite.

Resources