Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
1cf18343
Commit
1cf18343
authored
May 14, 2011
by
Fabien Potencier
Browse files
Options
Browse Files
Download
Plain Diff
merged igorw/https
parents
9fa8f9ba
09968780
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
129 additions
and
2 deletions
+129
-2
doc/services.rst
doc/services.rst
+25
-0
src/Silex/Application.php
src/Silex/Application.php
+7
-2
src/Silex/Controller.php
src/Silex/Controller.php
+20
-0
tests/Silex/Tests/Extension/UrlGeneratorExtensionTest.php
tests/Silex/Tests/Extension/UrlGeneratorExtensionTest.php
+77
-0
No files found.
doc/services.rst
View file @
1cf18343
...
@@ -196,6 +196,9 @@ of them.
...
@@ -196,6 +196,9 @@ of them.
$id = $app['request']->get('id');
$id = $app['request']->get('id');
This is only available when a request is being served, you can only access it
from within a controller, before filter, after filter or error handler.
* **autoloader**: This service provides you with a
* **autoloader**: This service provides you with a
`UniversalClassLoader
`UniversalClassLoader
<http://api.symfony.com/2.0/Symfony/Component/ClassLoader/UniversalClassLoader.html>`_
<http://api.symfony.com/2.0/Symfony/Component/ClassLoader/UniversalClassLoader.html>`_
...
@@ -234,6 +237,28 @@ of them.
...
@@ -234,6 +237,28 @@ of them.
Symfony2, it takes a Request as input and returns a
Symfony2, it takes a Request as input and returns a
Response as output.
Response as output.
* **request_context**: The request context is a simplified representation
of the request that is used by the Router and the UrlGenerator.
.. note::
.. note::
All of these Silex core services are shared.
All of these Silex core services are shared.
Core parameters
---------------
* **request.http_port** (optional): Allows you to override the default port
for non-HTTPS URLs. If the current request is HTTP, it will always use the
current port.
Defaults to 80.
This parameter can be used by the ``UrlGeneratorExtension``.
* **request.https_port** (optional): Allows you to override the default port
for HTTPS URLs. If the current request is HTTPS, it will always use the
current port.
Defaults to 443.
This parameter can be used by the ``UrlGeneratorExtension``.
src/Silex/Application.php
View file @
1cf18343
...
@@ -81,6 +81,9 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
...
@@ -81,6 +81,9 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
$this
[
'kernel'
]
=
$this
->
share
(
function
()
use
(
$app
)
{
$this
[
'kernel'
]
=
$this
->
share
(
function
()
use
(
$app
)
{
return
new
HttpKernel
(
$app
[
'dispatcher'
],
$app
[
'resolver'
]);
return
new
HttpKernel
(
$app
[
'dispatcher'
],
$app
[
'resolver'
]);
});
});
$this
[
'request.http_port'
]
=
80
;
$this
[
'request.https_port'
]
=
443
;
}
}
/**
/**
...
@@ -313,12 +316,14 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
...
@@ -313,12 +316,14 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
public
function
onCoreRequest
(
KernelEvent
$event
)
public
function
onCoreRequest
(
KernelEvent
$event
)
{
{
$this
[
'request'
]
=
$event
->
getRequest
();
$this
[
'request'
]
=
$event
->
getRequest
();
$this
[
'request_context'
]
=
new
RequestContext
(
$this
[
'request_context'
]
=
new
RequestContext
(
$this
[
'request'
]
->
getBaseUrl
(),
$this
[
'request'
]
->
getBaseUrl
(),
$this
[
'request'
]
->
getMethod
(),
$this
[
'request'
]
->
getMethod
(),
$this
[
'request'
]
->
getHost
(),
$this
[
'request'
]
->
getHost
(),
$this
[
'request'
]
->
getPort
(),
$this
[
'request'
]
->
getScheme
(),
$this
[
'request'
]
->
getScheme
()
!
$this
[
'request'
]
->
isSecure
()
?
$this
[
'request'
]
->
getPort
()
:
$this
[
'request.http_port'
],
$this
[
'request'
]
->
isSecure
()
?
$this
[
'request'
]
->
getPort
()
:
$this
[
'request.https_port'
]
);
);
$this
[
'controllers'
]
->
flush
();
$this
[
'controllers'
]
->
flush
();
...
...
src/Silex/Controller.php
View file @
1cf18343
...
@@ -110,6 +110,26 @@ class Controller
...
@@ -110,6 +110,26 @@ class Controller
return
$this
;
return
$this
;
}
}
/**
* Sets the requirement of HTTP (no HTTPS) on this controller.
*/
public
function
requireHttp
()
{
$this
->
route
->
setRequirement
(
'_scheme'
,
'http'
);
return
$this
;
}
/**
* Sets the requirement of HTTPS on this controller.
*/
public
function
requireHttps
()
{
$this
->
route
->
setRequirement
(
'_scheme'
,
'https'
);
return
$this
;
}
/**
/**
* Freezes the controller.
* Freezes the controller.
*
*
...
...
tests/Silex/Tests/Extension/UrlGeneratorExtensionTest.php
View file @
1cf18343
...
@@ -29,6 +29,23 @@ class UrlGeneratorExtensionTest extends \PHPUnit_Framework_TestCase
...
@@ -29,6 +29,23 @@ class UrlGeneratorExtensionTest extends \PHPUnit_Framework_TestCase
$app
->
register
(
new
UrlGeneratorExtension
());
$app
->
register
(
new
UrlGeneratorExtension
());
$app
->
get
(
'/hello/{name}'
,
function
(
$name
)
{})
->
bind
(
'hello'
);
$app
->
get
(
'/'
,
function
()
{});
$request
=
Request
::
create
(
'/'
);
$app
->
handle
(
$request
);
$this
->
assertInstanceOf
(
'Symfony\Component\Routing\Generator\UrlGenerator'
,
$app
[
'url_generator'
]);
}
public
function
testUrlGeneration
()
{
$app
=
new
Application
();
$app
->
register
(
new
UrlGeneratorExtension
());
$app
->
get
(
'/hello/{name}'
,
function
(
$name
)
{})
$app
->
get
(
'/hello/{name}'
,
function
(
$name
)
{})
->
bind
(
'hello'
);
->
bind
(
'hello'
);
...
@@ -38,6 +55,66 @@ class UrlGeneratorExtensionTest extends \PHPUnit_Framework_TestCase
...
@@ -38,6 +55,66 @@ class UrlGeneratorExtensionTest extends \PHPUnit_Framework_TestCase
$request
=
Request
::
create
(
'/'
);
$request
=
Request
::
create
(
'/'
);
$response
=
$app
->
handle
(
$request
);
$response
=
$app
->
handle
(
$request
);
$this
->
assertEquals
(
'/hello/john'
,
$response
->
getContent
());
$this
->
assertEquals
(
'/hello/john'
,
$response
->
getContent
());
}
}
public
function
testAbsoluteUrlGeneration
()
{
$app
=
new
Application
();
$app
->
register
(
new
UrlGeneratorExtension
());
$app
->
get
(
'/hello/{name}'
,
function
(
$name
)
{})
->
bind
(
'hello'
);
$app
->
get
(
'/'
,
function
()
use
(
$app
)
{
return
$app
[
'url_generator'
]
->
generate
(
'hello'
,
array
(
'name'
=>
'john'
),
true
);
});
$request
=
Request
::
create
(
'https://localhost:81/'
);
$response
=
$app
->
handle
(
$request
);
$this
->
assertEquals
(
'https://localhost:81/hello/john'
,
$response
->
getContent
());
}
public
function
testUrlGenerationWithHttp
()
{
$app
=
new
Application
();
$app
->
register
(
new
UrlGeneratorExtension
());
$app
->
get
(
'/insecure'
,
function
()
{})
->
bind
(
'insecure_page'
)
->
requireHttp
();
$app
->
get
(
'/'
,
function
()
use
(
$app
)
{
return
$app
[
'url_generator'
]
->
generate
(
'insecure_page'
);
});
$request
=
Request
::
create
(
'https://localhost/'
);
$response
=
$app
->
handle
(
$request
);
$this
->
assertEquals
(
'http://localhost/insecure'
,
$response
->
getContent
());
}
public
function
testUrlGenerationWithHttps
()
{
$app
=
new
Application
();
$app
->
register
(
new
UrlGeneratorExtension
());
$app
->
get
(
'/secure'
,
function
()
{})
->
bind
(
'secure_page'
)
->
requireHttps
();
$app
->
get
(
'/'
,
function
()
use
(
$app
)
{
return
$app
[
'url_generator'
]
->
generate
(
'secure_page'
);
});
$request
=
Request
::
create
(
'http://localhost/'
);
$response
=
$app
->
handle
(
$request
);
$this
->
assertEquals
(
'https://localhost/secure'
,
$response
->
getContent
());
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment