Is it be possible to make a website that doesn't reveal any relative URL's at all?
Say for example, I have a domain name "somedomain.xyz" and I want to route everything through the default route, and I want not to reveal any paths or route structures to the end user.
The end user shall only see the domain name in the browser's address bar, like:
http://somedomain.xyz
or
https://somedomain.xyz.
Any path like
http://somedomain.xyz/index.php
or
http://somedomain.xyz/index or
http://somedomain.xyz/index/index
shall show a 404.
And I don't care about SEO stuff and static pages.
Is that possible with ZF2, and if yes, then how?
similar question: hide module and action name from zf2 routing
Just create a hostname route for subdomain.xyz like so:
'my-route' => array(
'type' => 'Hostname',
'options' => array(
'route' => 'subdomain.xyz',
'defaults' => array(
'controller' => 'MyApp\Controller\TheController',
'action' => 'whatever-action',
),
),
),
see here for a complete solution, with using HTTP POST vars for the routing:
ZF2 routing via post vars
Related
Using ZF2 to customise an Entity based on ZfcUser. Trying to use ScnSocialAuth and got a bit of a problem.
The problem is that I am using custom routes ('/account' instead of '/user') and when implementing ScnSocialAuth I cannot get the social code into my custom zfcuser view...?
I have \\view\zfc-user\user\register.php which overrides the zfcuser registration.
I have a customised route:
'account' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/account',
),
),
These are my zfc config modification within \my-module\config\module.config.php
'zfcuser' => array(
// telling ZfcUser to use our own class
'user_entity_class' => 'WMember\Entity\WMember',
// telling ZfcUserDoctrineORM to skip the entities it defines
'enable_default_entities' => false,
'table_name' => 'w_member',
'login_redirect_route' => 'account',
),
My global \config\application.config.php
'ScnSocialAuth',
'MyModule1',
'ZfcBase',
'ZfcUser',
'BjyAuthorize',
'GoalioMailService',
'GoalioForgotPassword',
'my-user-module',
Therefore, after all this:
I can see my own extended User registration form by navigating to
/account/register with no Social login links visible
I can see the ScnSocialAuth when navigating to /user/register
a) I cannot create the view in my module to override \vendor\scn-social-auth\user\register.phtml as was done with zfcuser
Please help with getting ScnSocialAuth to work with my custom route setup.
If this is just wrong please let me know as I'm not ZF2 expert. Happy to take 'constructive' criticism.
Saw these posts: How to (correctly) extend ScnSocialAuth\Authentication\Adapter\HybridAuth::authenticate() method?
and this as a result of the above post:
https://github.com/SocalNick/ScnSocialAuth/issues/202
NOTE: still running ZF-2.3* due to PHP 5.3,5.4
Instead of adding a custom route to your config, you need to over-ride the zfcuser route
<?php
// #file MyUserModule/config/module.config.php
return array(
// other config ...
'router' => array(
'routes' => array(
'zfcuser' => array(
'options' => array(
// this is the only change needed to route zfcuser to /account
'route' => '/account',
),
),
),
),
// more config ...
);
The ScnSocialAuth module uses the forward() plugin to render the content from zfcusers register view (and login view iirc), which means it will only ever look at the zfcuser route and completely ignore your custom route. The only way to have it use your custom route would be to replace ScnSocialAuths UserController with your own using identical code but forwarding to your custom route (much more work there, and still the potential to break anything else that expects zfcuser to be the route used)
I typically format my routes in ZF2 like so: /name/to/route
Now I have been doing the same thing with my api routes however I am finding that I am struggling to include data such as encoded urls or arrays.
Here is an example of such a route:
http://example.com/api/register/access/code/c102dea422fa4bb6958d77a29d9873d2/http%3A%2F%2Frouter-local.example.com%2Fapi%2Fdirectory
The following represents forward slashes and thus causes the route not to work: %3A%2F%2
I am thinking I should encode my route as such:
http://example.com/api/register/access/code/?access_code=c102dea422fa4bb6958d77a29d9873d2&route=http%3A%2F%example.com%2Fapi%2Fdirectory
How do you configure the module.config file to deal with this?
Currently it is set as such in apigility:
'api.rpc.register-access-code' => array(
'type' => 'Segment',
'options' => array(
'route' => '/api/register/access/code/:access_code/:route',
'defaults' => array(
'controller' => 'Api\\V1\\Rpc\\RegisterAccessCode\\Controller',
'action' => 'registerAccessCode',
),
),
),
EDIT
I have encoded my routes to include GET parameters by doing the following:
$url = "http://example.com/api/register/access/code/";
$params = [
'access_code' => 'c102dea422fa4bb6958d77a29d9873d2',
'route' => 'http://example.com/api/directory'
];
$final = $url . "?" . http_build_query($params);
Which gives this:
http://example.com/api/register/access/code/?access_code=c102dea422fa4bb6958d77a29d9873d2&route=http%3A%2F%2Fexample.com%2Fapi%2Fdirectory
However this breaks due to a "The requested URL could not be matched by routing." error.
The route is unidentified due to the interpretation of the slashes in the included URL.
Perhaps the issue is to do with how the URL is formatted and included as a parameter?
You don't define query variables in the segment route option; only the path.
You may append ?query=vars to any url, regardless of route configuration. ZF2's url helpers should encode the query vars for you, you just have to create an array of query vars and give it to the helper function when creating a url.
<?php echo $this->url('api.rpc.register-access-code', array(), array('query' => array(
'access_code' => 'c102dea422fa4bb6958d77a29d9873d2',
'route' => 'http://router-local.example.com/api/directory',
))); ?>`
In this case, it would seem the problem is to do with htaccess or apache. The simplest solution has been to encode the url using: base64_encode($url) which can be de-coded at the other end.
I want to redirect some pages to the login page instead of page 403.
By default BjyAuthorize redirects everything to a 403 page. Is it possible to configure this behavior?
I found this: RedirectionStrategy. How do I use this?
Finally I got it.
With version 1.2.* of BjyAuthorize, you simply add in config/autoload/bjyauthorize.global.php :
return array(
'bjyauthorize' => array(
'unauthorized_strategy' => 'BjyAuthorize\View\RedirectionStrategy',
// [...]
),
);
And it will redirect you to the route configured in vendor/bjyoungblood/bjy-authorize/src/BjyAuthorize/View/RedirectionStrategy.php
Check this UnauthorizedStrategy class by Rob Allen: https://gist.github.com/akrabat/3783912
When using this class you have to configure BjyAuthorize to use it, like this:
return array(
'bjyauthorize' => array(
'unauthorized_strategy' => 'Application\View\UnauthorizedStrategy',
),
);
Edit:
Don't forget to add the relevant service manager config to allow the service manager to instantiate the UnauthorizedStrategy object:
'service_manager' => array(
'invokables' => array(
'Application\View\UnauthorizedStrategy' => 'Application\View\UnauthorizedStrategy',
),
I'm also trying and I came across this page: https://github.com/bjyoungblood/BjyAuthorize/issues/24
This way, you can extend the UnauthorizedStrategy.
I am working with ZF2 and trying to setup Route configuration that uses a colon separator.
For example, the web address could be www.example.com/namespace:subject and I want to send it to a specific controller, action with the two variables. I am trying to use a Regex since the colon ":" is a special character for segments. Is there a nice way to do this? Here is my route configuration:
'dataReqs' => array(
'type' => 'regex',
'options' => array(
'regex' => '/(?<namespace>[^:]+).(?<subject>[a-zA-Z0-9_-]+)',
'defaults' => array(
'controller' => 'Application\Controller\Data',
'action' => 'get',
),
'spec' => '/%namespace%:%subject%',
),
),
EDIT: I want to use the colon as the prefix:resource format is commonly used in RDF syntax (http://www.w3.org/TR/2007/PR-rdf-sparql-query-20071112/#QSynIRI). For instance, a long uri like http://dbpedia.org/data/Semantic_Web with a #prefix dbp: http://dbpedia.org/resource/ may be referred in a document with dbp:Semantic_Web. So for my Linked Data server I could direct requests and include the prefix (namespace) and the resource name; eg http://myserver.com/dbp:Semantic_Web. While I am using the segment combinations /namespace/resource for now, it would be nice to handle a route with prefix:resource syntax.
Do not use colon in your route. It isn't good practice, because colon is reserved character(see https://www.rfc-editor.org/rfc/rfc3986#section-2.2)
I'm inclined to agree with kormik. Why do you want to specify URL's in that way? What is wrong with the default behavior?
www.example.com/namespace/subject
eg:
www.example.com/somenamespace/10
or even:
www.exmple.com/namespace/namespace/subject/subject
eg
www.example.com/namespace/somenamespace/subject/10
you can easily grab these parameters in the controller like so:
$ns = $this->params()->fromRoute('namespace',0);
$subject = (int) $this->params->fromRoute('subject',0);
You would need to modify the route config also.
A plea for your indulgence. I have searched for answers and tried many things, so I now humbly turn here for help. It should be simple: I'm moving to CakePhp and I want to redirect my old query strings (action=show&id=2) to groovy cake URLs (/Feature/view/2).
I've tried this in the .htaccess file in the webroot:
RewriteCond %{QUERY_STRING} ^action=show&id=([0-9]+)$
RewriteRule /Features/view/%1? [R,L]
No love. I also tried:
RewriteRule action=show&id=([0-9]+) /Features/view/$1 [L]
No love.
I tried Cakephp's routes.php with:
Router::connect('index.php?action=show&id=([0-9]+)',array('controller' => 'features', 'action' => 'view', 'id' => $1));
But I've seen no evidence that regex can be used that way in routes.php so that was really just throwing up a prayer.
It's possible to do this. Right? Thanks for any advice!
I think you can do it within router.php!
Maybe, you can get away with:
Router::connect('?action=:action&id=:id',
array(
'controller' => 'myController',
'action' => 'myAction',
),
array(
'action' => '[a-zA-Z]+',
'id' => '[0-9]+',
)
);
Or (probably better) a series of more specific forms like:
Router::connect('?action=show&id=:id',
array(
'controller' => 'features',
'action' => 'view',
),
array(
'id' => '[0-9]+',
)
);
In this case, action and id would be available in $this->request->params in myController (and in the case of a standard like id, there might even be automagic to help!)
Though I'm not sure that the routing elements (:foo) will pick up GET params like that..
Alternatively, you could send everything to one controller anyway, and you should find the GET parameters are listed in $this->request->params['url'], so you can route everything in the controller (to other controllers, I guess).
Doesn't sound pretty either way, but I understand you want to keep some legacy urls running!