Symfony2: Full path to action/route in a controller - symfony

I need the full path to a action inside my controller, to send it via email. How can I achieve something like {{ path('_route') }} from inside my controller but the full path?

Juan's answer is right if you want the local path. The absolute path — which is helpful to be send through email — needs extra parameter(s):
$url = $this->generateUrl('your_route_name', array(), true);
The third parameter indicates that the absolute path is to be generated.
If you want to use this URL in your view just add the $url to the response array in your action and use it.

Symfony 3+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
$this->generateUrl('your_route_name', array('/* your route parameters */'), UrlGeneratorInterface::ABSOLUTE_URL);

Try the following:
$url = $this->generateUrl('your_route_name');

Related

How to get route name from path in Twig?

Is it possible in twig to get the route name from a path given (not the current one).
I know that to get the current route, it is like this :
{% set current_path = app.request.get('_route') %}
but this is not what I am looking for. I want to give another path than the current one.
Here is come code of what I want to do
With javascript (Mootools), I add an event on some buttons.
element.addEvent("click", function(event) {
event.preventDefault();
event.stopPropagation();
AjaxFormValideEtEnregistrement(element.get('href'));
});
function AjaxFormValideEtEnregistrement(href) {
//href is a path and I want to get the route..
if i create a route filter I cannot do this :
{{ href|route }}
I guess it is not possible .
}
Not possible out of the box. You are supposed to create your own Twig extension and develop a function, eg router_generate which will:
Match a route by provided path
Return the name of the matched route
Also, if you could explain better what's the use case for such functionality maybe we could assist you with some other, possibly better way of achieving your goal.

Laravel Basic Routing with no parameters - NotFoundHttpException

I have a custom route controller that checks the database before returning view. that being said I'm not passing any parameters to this controller besides the first and never will. Is there a way to stop laravel from expecting a parameter? I'm getting this error:
mydomain/login ---- Works Fine
mydomain/login/sometext -- Throws error
"Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException"
/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php
routes.php
Route::get('/', function(){return View::make('main.landing');});
Route::get('/{path}', array('uses' => 'RouteController#index'));
Then in my RouteController I take $path and query database to check if route exists and then displays the custom view.
Any help would be greatly appreciated!
Thanks!
You need to specify to the router that you want to allow slashes in your route parameter. You can do it like so:
Route::get('/{path}', array('uses' => 'RouteController#index'))->where('path', '(.*)');
This will allow any character.
By default route parameters do not accept slashes. You need to explicitly allow any characters in your path parameter:
Route::get('/{path}', array('uses' => 'RouteController#index'))
->where('path', '(.*)');

Wordpress : Module with the GET/POST value accept?

In Wordpress, how to make a API like Module, which can accept the data being passed via POST arguments. I mean, the Wordpress should be able to accept the URL calls from external, and then process it.
I mean, as the external Application or myself manually, when i call:
http://www.wordpress-site.com/test?name=james&age=14
Then how to write a module to read such incoming POST values and process it (Save them into the Database or something)
It is actually a public API.
Try with this one:
In Base page it should seems something like this :
Click Here
In Destination page it should seems something like this :
<?php if(isset($_GET['name']) && isset($_GET['age'])){
echo "name=".$_GET['name'];
echo "age=".$_GET['age'];
}
?>
Thanks.
Take a look on WordPress | Accept incoming url with variable parameters
that helps you to get the incoming parameters and wp action hook might be helpful that you want to achieve.
wp action hook

Drupal 7 views Contextual filters with Blocks, default value doesn't work properly , wield

I have a view created that show content based on the url: eg: domain.com/projects/[username]/[projectname], which shows a project of a specific user, that works well. Additionally, I want to show the related projects of this user on the sidebar, so I create a block view, using the Contextual filters and the default value.
because my url is projects/username ,By using the default value -> raw value from URL, so I set the path component as 2. but that doesn't work for me.
eg: suppose my username is "abc".
in the preview, if the url path is "projects/abc", it doesn't show anything although the username is in the 2ed component of url path.
if the url path is "abc/*", it will show the related content, meaning as long as the username is in the first argument of the url path, it works.
I don't what happens, it seems that configuration of path component as 2 doesn't work. I am so puzzled , what happened ?
Yo are missed something. I think each of your users have a url alias. For example 'user/1' have url alias 'abc'. Url components of contextual filter works for direct url, not for alias. For example second component of user url 'abc' will be '1', becauser original url is 'user/1'.
You can easily debug it by printing argument in working views header, try it yourself:
<?php print arg(0) . '<br>' . arg(1); ?>
For my user/1 that have alias 'abc' i received next:
user
1
For path 'abc/*' this is second component, but
for path 'projects/username' it is not 3-rd because this url hasn't aliases with [uid] component, you must use projects/[uid] or other validation for argument, for example you can check autor of a project. Choose a 'User ID from UR'L and check option 'Also look for a node and use the node author' in default value settings of contextual filter.

Drupal - Getting node id from view to customise link in block

How can I build a block in Drupal which is able to show the node ID of the view page the block is currently sitting on?
I'm using views to build a large chunk of my site, but I need to be able to make "intelligent" blocks in PHP mode which will have dynamic content depending on what the view is displaying.
How can I find the $nid which a view is currently displaying?
Here is a more-robust way of getting the node ID:
<?php
// Check that the current URL is for a specific node:
if(arg(0) == 'node' && is_numeric(arg(1))) {
return arg(1); // Return the NID
}
else { // Whatever it is we're looking at, it's not a node
return NULL; // Return an invalid NID
}
?>
This method works even if you have a custom path for your node with the path and/or pathauto modules.
Just for reference, if you don't turn on the path module, the default URLs that Drupal generates are called "system paths" in the documentation. If you do turn on the path module, you are able to set custom paths which are called "aliases" in the documentation.
Since I always have the path module turned on, one thing that confused me at first was whether it was ever possible for the arg function to return part of an alias rather than part of system path.
As it turns out, the arg function will always return a system path because the arg function is based on $_GET['q']... After a bit of research it seems that $_GET['q'] will always return a system path.
If you want to get the path from the actual page request, you need to use $_REQUEST['q']. If the path module is enabled, $_REQUEST['q'] may return either an alias or a system path.
For a solution, especially one that involves a view argument in the midst of a path like department/%/list, see the blog post Node ID as View Argument from SEO-friendly URL Path.
In the end this snippet did the job - it just stripped the clean URL and reported back the very last argument.
<?php
$refer= $_SERVER ['REQUEST_URI'];
$nid = explode("/", $refer);
$nid = $nid[3];
?>
Given the comment reply, the above was probably reduced to this, using the Drupal arg() function to get a part of the request path:
<?php
$nid = arg(3);
?>
You should considder the panels module. It is a very big module and requires some work before you really can tap into it's potential. So take that into considderation.
You can use it to setup a page containing several views/blocks that can be placed in different regions. It uses a concept called context which can be anything related to what you are viewing. You can use that context to determine which node is being viewed and not only change blocks but also layout. It is also a bit more clean since you can move the PHP code away from admin interface.
On a side note, it's also written by the views author.
There are a couple of ways to go about this:
You can make your blocks with Views and pass the nid in through an argument.
You can manually pass in the nid by accessing the $view object using the code below. It's an array at $view->result. Each row in the view is an object in that array, and the nid is in that object for each one. So you could run a foreach on that and get all of the nid of all rows in the view pretty easily.
The first option is a lot easier, so if that suits your needs I would go with that.
New about Drupal 7: The correct way to get the node id is using the function menu_get_object();
Example:
$node = menu_get_object();
$contentType = node_type_get_name($node);
Drupal 8 has another method. Check this out:
arg() is deprecated

Resources