need some help about codeigniter route - php

Is there a way to make the route in codeigniter work like that in symfony?
Cause in there you can rename the route path and it still wouldn't affect your output cause what you call is the route name not the route path unlike in codeigniter.
For example, in symfony, routes are set like this:
news_index:
path: /news
defaults: { _controller: CmsBundle:News:index }
news_view:
path: /news/view/{id}
defaults: { _controller: CmsBundle:News:view }
and then I can access that in my template by calling the route name, not the route path
<?php
// outputs: /news
echo $view['router']->generate('news_index');
// outputs: /news/view/1
echo $view['router']->generate('news_view',array('id' => $news_id));
?>
so if I change the route path it wouldn't affect the way i call it in my code because i'm calling the route name, all it will do is change the output:
news_index:
path: /articles
defaults: { _controller: CmsBundle:News:index }
news_view:
path: /articles/item/{id}
defaults: { _controller: CmsBundle:News:view }
still same code but output is changed
<?php
// outputs: /articles
echo $view['router']->generate('news_index');
// outputs: /articles/item/1
echo $view['router']->generate('news_view',array('id' => $news_id));
?>
while in codeigniter, if i set my route like this:
$route['news'] = 'NewsController';
$route['news/view/(:num)'] = 'NewsController/view/$1';
the only way i know to call it is by the route path:
<?php
echo anchor('news');
echo anchor('news/view'.$news_id);
?>
and if I change my path, I would have to change what I wrote in view as well. That would be a hassle. So I'm wondering if there could be a way to make it work like in symfony. I would appreciate it if anyone could help me. And I apologize if my explanation is not clear. English is not my mother tongue :)

There is blog post available from #kennyk that describes his approach to symfony-like named routes using a router extension.
The article is called Easy Reverse Routing with CodeIgniter.
I guess that's what you're looking for.

Related

Is it possible to construct a Symfony path using only parameters?

I would like to reduce the number of repetitive code and give a canonical URL in my Drupal 8 application. Since the routing system is built on Symfony, I included it in the title.
I am constructing paths under routes in my mymodule.routing.yml file. I want to match a specified number of different strings in the first argument, and a slug which can be any string in the second argument. It looks like this:
entity.my_entity.canonical:
path: '/{type}/{slug}'
defaults:
_controller: '\namespace\PostController::show'
requirements:
_permission: 'perm'
type: different|strings|that|can|match|
Now, when I try to access using for example /match/some-slug then it just says "Page not found".
If I something static to the path, for example path: '/j/{type}/{slug}', then it works as expected when I open /j/match/some-slug in the browser.
My boss doesn't like any unnecessary characters in the URL though, so I would like to achieve this by using two parameters, like shown in the first example.
As Yonel mentioned in the comments you can use debug:router to check all your routes. I don't see anything wrong with your code.
Try running bin/console router:match "/match/blaaa" and if you see some controller that isn't the one you want then you'll need to change the route. It shouldn't be the case though because you're getting a 404.
Here's my exact setup that works
routing.yml:
entity.my_entity.canonical:
path: '/{type}/{slug}'
defaults:
_controller: 'MyBundle:Something:foo'
requirements:
type: different|strings|that|can|match|
Inside MyBundle\SomethingController:
public function fooAction($id)
{
return new Response("bar");
}
Then going to http://localhost/match/fom shows the "bar" response.
I have read the documentation again (RTM), and found out that it is not possible in Drupal 8, while it is possible in Symfony.
Note that the first item of the path must not be dynamic.
Source: Structure of routes in Drupal 8

Symfony2 routes redirecting to wrong controller

In my symfony2 application I want specific routes for my pages, to work well with my seo, but I receive some serious problems and I dont understand them..
EXAMPLE:
Two routes:
blog_article:
path: /blog/{slug}
defaults: {_controller: ApplicationEDBlogBundle:Blog:singleArticle}
product:
path: /{category}/{name}
defaults: { _controller: MpShopBundle:Product:view}
The product route works fine, but the blog_article route always redirects to product route..
In my understanding if i open blog: /blog/firstBlog/ by default it thinks that the blog is a category and firstBlog is the product name, because my product route is the last route.
But if in my twig i specificaly tell which route to go to, shouldnt it work?
For example: {{ path('blog_article', {slug: blog.slug}) }}. Shouldnt this look at the blog_article route and open the needed controller? Or it does not work like that?
If so, how to keep my pretty urls the way I want to?
change routing to
blog_article:
path: /blog/{slug}
defaults: {_controller: ApplicationEDBlogBundle:Blog:singleArticle}
product:
path: /cat/{category}/{name}
defaults: { _controller: MpShopBundle:Product:view}
and will be fine .
In your example {category} could be "blog" , so 1st route was matched .
It can also work if you change order ( product w'll be first). But it's not good solution (what if someone add category blog ?)
Nope, it doesn't work like that, i.e. your example path code doesn't mean that the routing should look for the blog_article route:
The twig path function just expands the route into the actual url (/blog/yourslug) and when actually accessing that url, the system makes the matching the other way around from the url to the route (matching to whichever happens to be the first of the above listed two route definitions).
If you have this kind of routes, the solution is to have them neatly in the correct order (most generic ones - the product in this case - being always the last), or if the ordering is not possible, you can try to solve this by placing some specific route requirements if applicable.

Generate appropriate urls in multiple sub-domain hostname in Symfony2

I am developing a multi sub-domain web application in Symfony 2.
Subdomains:
admin.example.com is the main hosting site.
member.example.com is another sub-domain pointing to same source code.
As mentioned in the documentation here, I configured routing as below :
Parent Routing for member.example.com:
my_app_member:
resource: "#member.yml"
prefix: /
host: "member.example.com"
Note : The above routing is the parent route config for all routes for member.example.com defined in member.yml.
**Now I have anoter route for admin.example.com : **
admin_user_mod:
path: /admin/new
defaults: { _controller: "somecontroller" }
However if I have to generate a full url for the route admin_user_mod using code :
$modLink = $this->get("router")->generate('admin_user_mod');
The generated route path is correct but the base url still stays as member.example.com which should be admin.example.com
Is there an way, or I am missing anything in above route configuration to get the desired results.
OR
Is there any symfony event listener to overwrite router's "generate()" method call?
All your inputs are highly appreciable.
Thank you
Router has getContext() method you can use:
$context = $this->getContainer()->get('router')->getContext();
$context->setHost('admin.example.com');
As it says in the Routing Documentation, passing true as the third parameter for the generate method will generate an absolute URL.
So in your case it would be...
$modLink = $this->get('router')->generate('admin_user_mod', [], true);
Just make sure you specify the host in the route configuration when doing this. So for example your route should be...
admin_user_mod:
path: /admin/new
host: admin.example.com
defaults: { _controller: "somecontroller" }

access to locale variable from routing in Symfony2

I got the following issue. I am developing a project in which the routes will depend upon the locale variable. Currently is working but it has some issues that I would like to fix.
The structure I got is this one:
AppBundle
->config
->routing.en.yml #routes in English
->routing.es.yml #routes in Spanish
->routing.php #in charge of making the magic
#/app/config/routing.yml
app:
resource: "#AppBundle/config/routing.php"
type: php
fos_user:
resource: "#FOSUserBundle/Resources/config/routing/all.xml"
##AppBundle/config/routing.es.yml
about_us:
path: /nosotros
defaults: { _controller: AppBundle:Default:about }
##AppBundle/config/routing.en.yml
about_us:
path: /about_us
defaults: { _controller: AppBundle:Default:about }
The php file is invoked and this is what it does
//#AppBundle/config/routing.php
<?php
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
use Symfony\Component\Yaml\Yaml;
$collection = new RouteCollection();
$collection->addCollection(
$loader->import("#AppBundle/Controller/", "annotation")
);
$config = Yaml::parse(__DIR__."/../../../app/config/parameters.yml");
$locale = $config["parameters"]["locale"];
$routes = Yaml::parse(__DIR__."/routing.{$locale}.yml");
foreach($routes as $name => $data)
{
$collection->add($name, new Route($data["path"],$data["defaults"]));
}
return $collection;
It so far works but the problem is that is reading from the file and not from the session. I would like to know how can I inject that value from the session
Once again i am plagued with my lack of reputation and cannot just leave a comment on your post.
From what I understand, you want to make some routes available only for a given locale. For example, a french user will be able to access the route /bonjour but an english gentleman will only have the equivalent route /hello.
It is a bit weird. Why not make all routes available and change the locale to match the route? For example our french fellow will have a page in english if he tries to access the /hello url. Seems a lot more simple and handy.

Symfony2 Controller for Uri is not callable but the Uri is ok, what's happen?

I have a little problem, I've just found this problem in other users, but the solution is not working for me, because apparently my code is right:
I have this route:
rs_shoppingcart:
pattern: /carrito
defaults: { _controller: rsBundle:Default:shoppingcart}
And this is the Controller:
public function shoppingcartAction(){ ...
...
if($peticion->getMethod() == 'POST'){
...
And basicly, It's a form, first time I can go to the "carrito" route, but when I submit the form, It give me the next error:
The controller for URI "/carrito" is not callable.
What do you thing?
Thank you in advance
Probably the problem occurs in this line:
defaults: { _controller: rsBundle:Default:shoppingcart}
in reBundle a namespace is missing - you should add ther the parent directory of your bundle. In example - GeneralRsBundle:Default:shoppingcart
Make sure shoppingcart matches the string (not ignoring case) before Action in your Controller method.

Categories