Comments (12)
from phprouter.
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.
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
here is the view when i go to /links
heres where the confusion is on how i access that page using the name or using an a link
i want to know how i make that route on the json redirect to the page in question
from phprouter.
Also can i use {id} {username } etc within the json links
from phprouter.
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.
from phprouter.
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.
from phprouter.
So i found a solution which i think is the same as what laravel and other framework do is the following.
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.
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.
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.
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)
- Container: Reflection failed HOT 3
- Middleware HOT 5
- Obtaining QueryString Data HOT 5
- Middleware: inject header in next HOT 10
- Is it possible to pass parameters to middleware? HOT 2
- Can you nest middleware? HOT 1
- About $url->make() HOT 2
- Get current request object or at least requested url, when 404 or any other error occurs? HOT 1
- Question about obtaining list or urls from web file and the method type HOT 7
- Uncaught ReflectionException HOT 1
- اجرای route بعدی HOT 1
- How to pass the 404 pages as view HOT 1
- Group routes: Unable to access route where path is set as prefix root HOT 3
- Feature request: Override method
- QueryStringData Revisited HOT 3
- Language prefix HOT 2
- Distinguish between HTTP 404 and 405
- PHP Version
- Single Route file for multiple controller files/classes HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from phprouter.