Code Monkey home page Code Monkey logo

Comments (12)

mbamber1986 avatar mbamber1986 commented on May 22, 2024 1

from phprouter.

miladrahimi avatar miladrahimi commented on May 22, 2024

The error occurs when PhpRouter cannot find a route with the given name, so make sure your routes have name when you use Url::make() method. Just like the example, there is two routes defined, about and post, about route is simple but post route has a paramater so when you call Url::make() you have to pass the appropriate value for the route paramter. If it didn't help you please insert your routes list and the related controller code in the reply.

from phprouter.

mbamber1986 avatar mbamber1986 commented on May 22, 2024

hi thanks for the response i have actually put my own route in and the address url i set to link does that need to match an exisiting route

heres my routes with a name of gallery

image
here is the view when i go to /links

image

heres where the confusion is on how i access that page using the name or using an a link

image

i want to know how i make that route on the json redirect to the page in question

from phprouter.

mbamber1986 avatar mbamber1986 commented on May 22, 2024

Also can i use {id} {username } etc within the json links

from phprouter.

miladrahimi avatar miladrahimi commented on May 22, 2024

As for the generated link, the gallery route path has the {username} parameter so when you wanna make its url, you should pass a value for it like this:

echo $url->make('gallery', ['username' => 'jack']); // Result: /profile/jack/gallery

As for the not found page, the URL is /links/gallery, there is no such route in your routes, don't know where have you made that.

You might misunderstand the named routes feature.
Named Routes feature is for making the code depneded to the names instead of URLs.

For example if we have this route:

$router->get('/blog/posts/{pid}/comments/{cid}/edit', [PostsController, 'show'], 'blog.posts.comments.edit');

To generate /blog/posts/13/comments/666/edit,

Your code could be:

$link = str_replace('/blog/posts/{pid}/comments/{cid}/edit', '{pid}', 13);
$link = str_replace($link, '{cid}', 666);

or could be:

$url->make('blog.posts.comments.edit', ['pid' => 13, 'cid' => 666]);

So it's about generating links not redirecitng.

Please let me know if your problem is not solved yet.

from phprouter.

mbamber1986 avatar mbamber1986 commented on May 22, 2024

from phprouter.

miladrahimi avatar miladrahimi commented on May 22, 2024

Named routes has no direct relation with controllers, it's just for setting a name for a route, then using name in your codes instead of URLs.

What do you mean by "call"?

Do you want to redirect to a route?

use Laminas\Diactoros\Response\RedirectResponse;

$router->get('/dashboard', function() { /* ... */ }, 'dashboard');
$router->get('/login', function() { /* ... */ });
$router->post('/login', function (URL $url) {
    // If username and password is ok then:
    return new RedirectResponse($url->make('dashboard'));
});

Do you want have the link in the view layer:

use MiladRahimi\PhpRouter\Url;
use MiladRahimi\PhpRouter\View\View

class HomeController {
    public function show(Url $url, View $view) {
        return $view->make('home', ['url' => $url]);
    }
}
<a href="<?php echo $url->make('profile') ?>">Profile</a>

If you something else, please put the laravel code you mean then I can change it to PhpRouter ;)

from phprouter.

mbamber1986 avatar mbamber1986 commented on May 22, 2024

from phprouter.

mbamber1986 avatar mbamber1986 commented on May 22, 2024

So i found a solution which i think is the same as what laravel and other framework do is the following.

image

i was reading your code litterly as thats how it was supposed to be done in json thats my bad also i got myself confused on how to output the result so i could use it in a hyperlink or form data output,

what i was trying to do was pull the get value from {username} in my blade templating which wasnt possible anyway my solution was to pull $user->username in your code which works.

sorry for being a pain i really appreciate the work you have done here is a really decent little router.

just out of curiosity would it not be better to name make() as route() or would that just be to confusing :)

thanks for your time and patience with me i know what im doing now

from phprouter.

miladrahimi avatar miladrahimi commented on May 22, 2024

You are very welcome, it makes happy when it seems useful to someone.
As for naming suggestion, actually, if it was a helper function the name route() would be better but it's a method of the class Url so I think Url::make() makes more sense than Url::route() while Url and route are almost the same entity :)
But I would be happy to hear your ideas to make the package even better.

from phprouter.

mbamber1986 avatar mbamber1986 commented on May 22, 2024

Sorry again to open this i have no issues but i did come up with a suggestion if you think it would work and if you implement it to the system (it may be unrelated) but i was working on my project today and i thought about the routing names and a breadcrumb feature.

would this be possible is it something that might be worth looking into ??

from phprouter.

miladrahimi avatar miladrahimi commented on May 22, 2024

As far as I know, breadcrumb is completely a view layer problem, would you please tell me what's you idea?
for example the code (method) you believe should be there in Router class

from phprouter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.