so i want to expand my site to have like region site, so when user open my site it will be ask to select which region he/she interested into and lets just say he interesting in London, then the site will generate a webpage that already all content related to that region...
i tried to just do the same way i create administrator page by group them in this route
Route::group(array('prefix'=>'admins','before' => 'auth'),function(){
Route::resource('partner','AdminPartnerController',array('except' => array('show')));
Route::get('partner/index_kategori/{id}',array(
'as' => 'admins.partner.index_kategori',
'uses' => 'AdminPartnerController#index_kategori'
});
but it's mean that i need to create a different controller function for each region i gonna create, so it's not very efficient.
what comes to my mind is just like making
www.websiteadress.com/region
so how i can catch that "/region" and add that value into each my function in controller? how to build that kind of routes?
update:
to make it more easily to understand, what become my problem is like i have this normal route:
Route::get('product/{id}',array( 'as' => 'product','uses' => 'PublicController#product'));
what this route do is pretty straight foward, when i type
www.websiteadress.com/product/12
it will open the product page for number 12, so my problem is if i add like this
www.websiteadress.com/Asia/product/12
how i make my PublicController to catch Asia and later in my product function i will just process that..?
Of course you don't need to create controller for each region. You want to use simple route with a variable:
Route::get('region/{region}', 'RegionController#show');
And just one controller for all regions:
public function show ($region)
{
// Logic here
return view('region.show', compact(['region']));
}
So, when user will load http://example.com/region/London, show action will have London in $region variable.
so after searching for days and asking around, i finally getting the best solution to answer my own question and i willing to share it to here, so someday maybe there is other people who want to do what want to...
so first in the route just create like this
Route::group(['prefix' => '{region}'], function(){
Route::get('member',array('as' => 'member','uses' => 'PublicController#member'));
});
and what this route do is just group it into prefix with value is in region variable and then i set the get route to where the controller will use to show the page and also taking region value into use
and then in my PublicController
public function sitemap($region){
if($region =='london'){
//do something
}else{
//do something
}
}
and so there it is.. i can get those region value from prefix and use it. in my case i use it to show certain page with different content.
Related
I have a SEO project with Laravel, I want to use the routes to config a friendlys dynamic urls.
This is my route:
# Designs
Route::get('/d/{article}-{tag}-{design_name}-{design_id}',['as' => 'web.product_design', 'uses' => 'ProductController#getProductDesign']);
I want to build this SEO friendly url: /d/mug-harry-potter-wingardium-leviosa-xfdsfsdf
And that's what I call the route into any laravel blade view:
route('web.product_design',['article' => 'mug'), 'tag' => str_slug('Harry Potter'), 'design_name' => str_slug('Wingardium Leviosa'), 'design_id' => 'xfdsfsdf'])
The problem is that inside the ProductController I don't receive these parameters as I would like. I think Laravel confuses when it starts and when it finishes the slugs.
For example, in the controller method...
# Product Design Page
public function getProductDesign($article,$tag,$design_name,$design_id) {
dd($article); // It gives me back 'mug', that's right.
dd($tag); // It return me 'harry', is WRONG, I want to get 'harry-potter'.
dd($design_name); // It return me 'potter', is WRONG, I want to get 'wingardium-leviosa'.
dd($design_id); // It return me 'wingardium-leviosa-xfdsfsdf', is WRONG, I want to get 'xfdsfsdf'.
}
How can I build a url SEO friendly and at the same time be able to take the parameters correctly within the controller method?
If you got this in your code
mug-harry-potter-wingardium-leviosa-xfdsfsdf
and exploded it on - then you would be in the same boat. How would you know that harry-potter was a single entity and not two. If you want to have spaces in your parameters, and then slugify them, then you need to choose a different separator in the rest of the URL.
You could switch to _ instead?
Route::get('/d/{article}_{tag}_{design_name}_{design_id}'
so your url is now
mug_harry-potter_wingardium_leviosa_xfdsfsdf
Two routes
Route::get('{page}', ['uses' => 'PageController#show']);
Route::get('{city}', ['uses' => 'CityController#show']);
Route model bindings
$router->bind('page', function($key, $binder) {
return Page::firstByUrl($key);
});
$router->bind('city', function($key, $binder) {
return City::firstByUrl($key);
});
How call (enter in the) CityController (and call city bind) if Page model does not find?
always call only PageController, may be "middleware" helps me or another way
You can't, at least not with your current route definitions. That's because {page} and {city} mean the same thing as far as the router is concerned. It's the same as if they were {param} and {param}, which means that those two route definitions are equal as far as matching goes and it will always match the first one defined, which in your case will always call PageController#show.
The name you give them in the route definition is only to help you identify the parameter. So for example if you were to access the following two URLs:
http://example.com/about
http://example.com/london
The router can't possibly know which of those is a page name and which is a city, because to the router about and london are variable values, nothing more.
Instead you should find a way to differentiate the two, something like:
Route::get('pages/{page}', ['uses' => 'PageController#show']);
Route::get('cities/{city}', ['uses' => 'CityController#show']);
Now the router will know that after pages comes a page name and after cities comes a city name and the following will work just fine:
http://example.com/pages/about
http://example.com/cities/london
You can read more about the subject in the Laravel Routing Documentation.
So basically this is what I need. I have a router definition like this.
Route::get('view/{postId}', 'PostController#view');
Above router definition will get triggered if the url we request is www.domain.com/view/4. But what I want is I want to appear this url like www.domain.com/[category-of-post]/[titile-of-post] (Ex : www.domain.com/music/easy-chords-in-guitar).
In PostController, I will get the post using the id passed and thus can generate the url what I need. Here is the problem begins. As you can see, I need to redirect to a dynamic url which will look differently for each post. I want to redirect to this dynamic urls but these urls are not defined inside routes.php.
Is this possible in Laravel ?
In short, what I need is, I want to update Illuminate\Routing\RouteCollection::$route array with my dynamically generated url value with corresponding controller action in run time before I am invoking Redirect::to('someurl')
If you need further clarification, I will do it for sure. Please give me your suggestions.
it is simpler than you are thinking.
Route::get('{category}/{title}',['uses' => 'FooController#bar']);
This should be the last route defined in your route list. Any other route should go upper than this one.
this will match www.domain.com/music/easy-chords-in-guitar
rest routes define as you want.
e.g.
Route::get('/',['uses' => 'FooController#home']);
Route::get('about',['uses' => 'FooController#about']);
Route::get('contact',['uses' => 'FooController#contact']);
Route::get('{category}/{title}',['uses' => 'FooController#bar']);
route :
Route::get('action/{slug}', 'HomeController#actionredeemvoucher')->name('home.actionredeemvoucher');
Function in controller:
public function actionredeemvoucher($slug)
{
print_r($slug);
}
I'm trying to implement a very simple page tracking system with Laravel, just to know which pages are most accessed.
At first I thought of create a table with access date, request URL (from Request::path()) and user id, as simple as that.
But I'd have to show page titles on reports, and for that I need some way to translate the request URI to its page title. Any ideas for that? Is there a better way to accomplish this?
Currently I set page titles from Blade view files, through #section('title', ...).
Thank you in advance!
You can use Google Analytics to show pretty and very efficient reports. With the API, you can also customize the reports and (for example) show the pages titles.
If you want to develop it by yourself, I think that the best solution is to write an after filter that can be called after each page loading. One way of setting the title, in this case, is to use flash session variable (http://laravel.com/docs/session#flash-data) :
// routes.php
Route::group(array('after' => 'log'), function()
{
Route::get('users', 'UserController#index');
}
// filters.php
Route::filter('log', function() {
Log::create(array('title' => Session::get('title'), '...' => '...'));
}
// UserController.php
public function index()
{
Session::flash('title', 'Users Page');
// ...
}
// layout.blade.php
<head>
<title>{{ Session::get('title') }}</title>
...
I know this has already been answered but I would like to add that I've published a package that can be easily implemented to track page views for Laravel (if they are Eloquent ORM instances) in specific date ranges: last day, week, month or all time.
https://github.com/marcanuy/popularity
Within my index page I plan to have data from more than one model. For example, a list of users from the users model, and a list of recent updates from a posts model in two separate areas of the page. How would I go about doing this in the best way possible? From doing a little research it seems that maybe elements are what I'm looking for, but I'm unsure.
I'm using cake 2, by the way.
The short answer to your question is, just use `$this->loadModel('MyModel'); and you'll have access to any/all models you'd like for your index page.
OR
The long answer, on how to set up a "homepage" which accesses lots of models:
You can make a DashboardsController (or whatever you want to call it), then in the Dashboard model, you specify that you don't need a database table: var $useTable = false;
In the Config/routes.php file, add: Router::connect('/', array('controller' => 'dashboards', 'action' => 'index')); to make that your homepage (if you want to).
Then, in the Dashboard controller's index action, you can use $this->loadModel('Whatever');, and you're good to go to get data from that model: $myData = $this->Whatever->find('all');. You can load as many models as you'll need the data for.
TLDR / simplified:
1) Make Dashboard controller with 'index' action
2) Make Dashboard model and specify: var $useTable = false;
3) Set Route to use your Dashboard controller for homepage (or any other page:
`Router::connect('/', array('controller' => 'dashboards', 'action' => 'index'));`
4) Use $this->loadModel('Whatever'); to gain access to that model's methods