How to take part of url and store it into a variable?

For example:

/home/pages/id --> I want to take id out and store it into $id.

How to do that?

I try this:

$parts = parse_url($_SERVER['REQUEST_URI']);
$path_parts= explode('/', $parts[path]);
$id = $path_parts[1];

Use of undefined constant path - assumed 'path' (View: C:\xampp\htdocs\soulfy_repo\framework\resources\views\soulfy\pages.blade.php)

Member Avatar for diafol

Place quotes around path index.

Nevermind, I finally get it:

$url = $_SERVER['REQUEST_URI'];
$pos = strrpos($url, '/');
$id = $pos === false ? $url : substr($url, $pos + 1);   

$pages2 = \Soulfy\Pages::where('id', $id)->first();
Member Avatar for diafol

If you are using Laravel (as in your previous posts), why do you need this? Your router (routes/web.php) and various controllers should do this for you surely? For example:

Route::resource('pages', 'PageController');

See https://laravel.com/docs/5.5/controllers

$id = $pos === false ? $url : substr($url, $pos + 1);

So you're getting the last '/' in the url and then returning the remainder of the string. You are then using this blindly in a query. Could be messy. If you follow the Laravel pattern:

GET        /photos  index         photos.index
GET        /photos/create         photos.create
POST       /photos                photos.store
GET        /photos/{photo}        photos.show
GET        /photos/{photo}/edit   photos.edit
PUT/PATCH  /photos/{photo}        photos.update
DELETE     /photos/{photo}        photos.destroy

You can see that you could have a whole variety of "last items", not necessarily {id}.

Maybe I misunderstood the usage.

I am trying to create another page system similar to a blog on the front page.

Therefore, I have query the article based on id on the url. Also create a navigation system based on id for different articles.

Member Avatar for diafol

Blog systems usually use re-written "slugs" (sort of titles stripped of special characters) as opposed to ids in the urls. This helps with SEO. I imagine that the same would hold true, in that you could use the laravel router/controller method with this:

www.your-url.com/posts/whatever-the-title-of-this/

Notice the last '/' - very often we see this - your strrpos function would identify this as the last '/' and looked for the following string (which doesn't exist). One good reason to avoid DIY functions for urls - using Apache rewrites and routing can be much easier - although it doesn't appear that way if you're not used to it.

This could be resolved to something like: www.your-url.com?postTitle=whatever-the-title-of-this

Routing via 'resource' to a controller could give:

Route::resource('posts', 'postsController');

So the above url would map to...

GET        /posts/{postTitle}        posts.show

So in your controller...

public function show($postTitle)
{
    //whatever
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.