Laravel Automatic Adding Resource Directory to URL? - php

I need some help. I'm new to laravel and I'm experiencing some weird things.
I have a "users" resource inside views/users. Inside it are index.blade.php andcreate.blade.php.
My app url is http://laravel.dev/ so basically if I need to access the "Create Users" form I will go to http://laravel.dev/users/create the view loads fine but when I tried to click a link on my navbar it automatically adds "users" on the url even if there's no "users" in the nav url link. Example.
Dashboard
When I click it, it will add "users" in the beginning of the url automatically making it users/dashboard and as expected I get a 404.
My navbar is being loaded from other php file in my header.php
Here's my route
/*Route for Users*/
Route::resource('users', 'UsersController');

I would suspect that you are using page relative links, correct? In a nav I've always found it best to use root specific links so "/dashboard" vs. "dashboard."
If you are new to Larvel you may want to take a serious look at the rooting options before going to far into your project just because if you decided to go a different way, namely routing through the controllers (http://laravel.com/docs/controllers#resource-controllers) With a large app it can be a far better way to go, but the refactoring can be tough after to you have a complex enough app to justify it.

Related

Passing multiple parameters with laravel routes but keep the url "clean" (without showing them)

I'm working on an application using php framework Laravel.
I've encountered a problem since I need to open a modification form with two parameters. I pass them in my url like so:
<tr class="beBlack" onclick="document.location = '/showGriefs/{{$data->No_Grief}}/{{$data->No_Employe}}'">
(I know that it is sketchy to create a link within a table but I need the url to change according to which row is clicked/selected)
and I receive them in the following route:
Route::any('/showGriefs/{No_Grief}/{No_Employe}', 'GriefController#showGrief')->name('showGriefs');
My problem is that I don't want my url to change, because, with these url changes, my application can't find the files (CSS, JS and Plugins). And since laravel is using the public directory to store all those files, it's destroying my page. The only errors I get are some missing files error.
I've searched the internet a lot but didn't found anything, I hope you'll all be able to help me. Thanks.
I know 2 solutions to fix your problem. No need to change route URLs. Just fix asset problem.
1. Use / before asset url
If you use / before url, browser will try to look at resource on the root dir.
For example: if you need http://example.com/assets/img.png then use /assets/img.png. Changing URLs will not affect the asset URLs that starts with / character.
2. Use url helpers for asset URLs
asset('assets/img.png') will change asset url from assets/img.png to http://example.com/assets/img.png

Laravel 5 route returns view but changes url

Im not even sure if this is possible but what I am trying to do is this. I store different page modules in a database. When someone goes to a page with one of these modules, the pageController recognizes its a module and ( using the redirect path stored with this module in the DB ) redirects them to a get route which is how I know what info to pull and put on the page.
For instance, if someone was to go to /photo-album the pageController would recognize that was a page module and returns the redirect ( redirect/photo/albums ) and then redirects them to that route.
Route::get('redirect/photo/albums', ['uses' =>'PageController#getPhotoAlbums']);
The problem is the url then becomes /redirect/photo/albums. I would like it to maintain /photo-album
The reason I am doing it this way is because there will be several modules stored and each one will contain different things ( think blog, photo albums, video gallery etc. ). I need the redirect to figure out what goes on that page and what view to show. In this case PageController#getPhotoAlbums goes to the getPhotoAlbums method, pulls the photos and serves up the photoalbums view.
There may be a better way to do this and i'm open to it. Thanks in advance.
I don't have enough rep to comment, so I'll put this as an answer and hope it helps:
You really shouldn't get data from another controller. All your data should be available in the Eloquent model. This is important to the 'MVC' architecture.
There is more discussion on this here: Laravel: Load method in another controller without changing the url
So essentially, when your controller recognises it as a page module it should pull that data from the DB using Eloquent and then pass that to a View. That will mean that the original request URL is still retained.

Angular routing under wordpress site

I have a WordPress site where one of the pages that WordPress serve is an SPA based on AngularJS. The Angular-app uses html5mode so that I can browse to http://localhost/79133/71 and I'm still showing index.html. Now I want to deploy this to my WordPress site so that one of the pages under WordPress (the main page) is my SPA. However, if I browse to http://example.com/79133/71 WordPress will try to find the page with the permalink 79133/71 and won't understand that the main page should be served. How can i configure WordPress so that these new "routes" will be pointing to the first page?
I'm not an expert on WordPress, but in order to get html5mode to work in angular:
Any route starting with /your_spa needs to serve the main page (index.html) of your app.
You must ensure that any resources referenced in your spa (for example, /assets/themes/theme.css) needs to point to the correct location, even if your location url is your_spa/your/spa/page. This means no relative paths unless you use the base tag.
Here's a tutorial on how to configure the router in wordpress. I don't use wordpress so I'm sorry that I
It seems like a lot of trouble, and I would just use the default hash routes if possible.
I had the same basic issue, and solved it. Your situation is slightly different but I think you need something like:
function spa_rewrite() {
add_rewrite_rule('79133/71', '/', 'top');
}
add_action('init', 'spa_rewrite');
In practise you'll need to use some regex to make this more flexible

PHP MVC - Where/when to load shared page content ('modules'/'widgets')?

I'm trying to build a simple PHP MVC framework. What I have right now is a basic set-up with a router that sends the request to the right controller, so I'm making the URL
/blog/[id]
Resolve to a class method like
Blog->singlePost();
Within singlePost(); I then interact with the model to get the right blog post and send it off to the view. The basic MVC triad is all there.
But pages are more complex than this. I've got a sidebar that I need to fill with data (from the database and other sources). I've got a main menu to build, I need to show the details for the current logged in user, and more. But, that said, I don't need all of these sections on every page.
My question is, where do I initiate/build all these various shared sections for the view?
My options that I can think of are:
Have every controller send a request for the required additional sections to be built (would have to update every controller each time something is added, no way)
When mapping the URL's, define a group that the route belongs to that loads in all the required sections (Not ideal, not every section will be needed on every page, could get messy)
Have the View (or Template) embed these sections on-demand. So the data for these sections would not be retrieved until the view explicitly asks for the data (The basic functionality of this seems quite appealing, but does it break the rules of what the view should do?)
Something else?
3 Seems quite appealing to me because I'd like for my templates (that will be dynamic and switchable via a template system) to be able to request a section on any page. So one template may show the "Top 10 Blog Posts" on the homepage, but another may not, so there's no point in loading it. I see that with Symfony/Twig, you can do this: http://symfony.com/doc/2.0/book/templating.html#embedding-controllers
Is this a good idea for various modules on a site? Or should everything be loaded before the view starts being processed?

How to build CodeIgniter site correctly

I'm building a website using CodeIgnitier.
My problem is how can I use the MVC pattern inside a webpage.
Let's say I have a controller called "Settings", Within this page I want to divide the settings into categories, with tabs, like "System", "User" and so on.
I know how to do this in the usual way, something like "settings?tab=system" and then load the proper view according to the tab selected. But I want to make a separate MVC pattern for this settings page, is it possible? and how?
Basically the thing you are looking for is can be controlled using routes.ini or Route.php. The tab can be your querystring. Settings can be a controller and u can have an action called index which can be controlling it.

Categories