Patroklo/codeigniter-static-laravel-routes

Generating url from routes

afbora opened this issue · 10 comments

How can i generate url (reverse route) from routes?

For example:

echo Route::url('post', array('news', 47, 'hello-world')); // Output url: /news/47/hello-world/

I'm very sorry but I don't understand what do you want to achieve... Do you want to use an array instead of a normal url like:

Route::post('news/47/hello-world'...

Or do you want to use Route also as a library to get a complete url from this array?

Yes, i want to get a complete url from this array?

This library is only for redirecting urls, not to create new ones, for that you should use the URL helper, BUT, it only has support for GET method. I think that there is no official support to make non get links in codeigniter as there is in Laravel or Yii2.

You can see the url helper here

And I think that the method you can use is:

$segments = array('news', 'local', '123');
echo site_url($segments);

for example.

If I don't have fully explained what you wanted, tell me and I'll reopen the issue and continue talking about it.

I now how generating url with site_url(). I want to generate url from dynamic routes.

http://laravel.com/docs/5.1/routing#basic-routing

Generating URLs To Routes

You may generate URLs to your application's routes using the url helper:

$url = url('foo');

No, this library doesn't support the generation of urls because the helper already exists in codeigniter and does that. I don't see any diference between the site_url() and the url() methods from CI and Laravel.

For example my expecting:

Sample route of blog post:

Route::get('post, ''blog/(:num)/(:any)',  'blog/$1/$2');

Expected function:

$url = Route::url('post', array('news', 47, 'hello-world'));

Usage and Output:

 <a href="<?=site_url($url)?>">Hello world post!</a>

Output:

<a href="http://www.example.com/news/47/hello-world/">Hello world post!</a>

This is possible? Thanks @Patroklo

Sorry about the delay, I've been awfully busy this week.

Maybe you'll find useful the named routes:

https://github.com/Patroklo/codeigniter-static-laravel-routes#named-routes

They do more or less the same thing, also you don't have to worry about storing the $url variable from the location where the Route library it's used and where you need to use it. Also they support parameters like the ones you are using in the expected function.

That would be what you need?

edition: example

Route::get('user/{id}', 'user/load/$1', array('as' => 'user'));

Then in your controller:

<a href="<?=site_url(Route::named('user', array('12')))?>"> Hello worldo!</a>

I think that will be useful for you. It's not exactly as Laravel works, but keep in mind that I made this library from an old version of laravel (I think that it had this feature of named routes back then? I haven't used Laravel in ages) and also, CI doesnt work like that framework at all, we have to make concessions.

That's what I was looking for! 👍 Thanks @Patroklo

I'm using routes on routes.php file like that:

Route::get('user/{id}', 'user/load/$1', array('as' => 'user'));
$route = Route::map();

Firstly routes then map() it.
Is that right usage?

Hi

You have to pass the Route::map() into the variable that stores the routes before sending them into CI, usually it's $route, yes.